Passing multivaluearray from php to javascript after ajax call The passing of the array works only one time. After an ajax call the new array is not passed to drupalSetting only the 1st array will displayed multiple times in the console. I tried it with a static test array, with a dynamic array, with json_encode() and such other things.

My PHP script:

$form['#attached']['drupalSettings']['variable'] = $vars;


devel:

Array (
    [0] =&gt; Array (
            [0] =&gt; val1
            [1] =&gt; val2
            [2] =&gt; val3
        )
    [1] =&gt; Array (
            [0] =&gt; val1
            [1] =&gt; val2
            [2] =&gt; val3
        )
....
)


My jQuery script:

(function($, Drupal, drupalSettings) {
    Drupal.behaviors.vShop = {
        attach: function (context, settings) {

            var loadVar = function(event, request, settings) {              
                var varArr = drupalSettings.variable;
                console.log(varArr);
            };

            $( document ).ready(loadVar);
            $( document ).ajaxComplete(loadVar);
        }
    };
})(jQuery, Drupal, drupalSettings);


console:

Object { 0: {…}, 1: […] }


I think the main issue is that the drupalSettings.variable don't get the new array from the form after the ajax call.The attached values in the form are changed correctly.
I searched on the wrong position, after i found:
http://www.thecarneyeffect.co.uk/drupal-8-ajax
I got the solution here
https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Ajax%21SettingsCommand.php/class/SettingsCommand/8.2.x

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\SettingsCommand;

function ajaxCallback(array &amp;$form, FormStateInterface $form_state) {

    $form_state-&gt;setRebuild();

    $myValue = $form_state-&gt;getValue('myValue');

    $ajax_response = new AjaxResponse();
    $ajax_response-&gt;addCommand(new SettingsCommand([
       'variable' =&gt; $myValue,
    ], TRUE));

  return $ajax_response;
}