Tell me if there is a function in js / jquery for merging two arrays into one, as follows, so that the value of the first array is a key, and the value of the second is the value of this key, for example

var arr1 = ["first","second"]; var arr2 = ["1","2"]; var newArr = ["first":"1","second":"2"]; 

Thank you all figured out !!!

    2 answers 2

    There are no associative arrays in js. Instead, objects are used:

     var arr1 = ["first","second"]; var arr2 = ["1","2"]; var map = {}; for (var i = 0, l = arr1.length; i < l; i++) { var key = arr1[i]; var val = arr2[i]; map[key] = val; } 

       $(function() { var arr1 = ["first","second"], arr2 = ["1","2"], newArr = {}; for(var i = 0; i < arr1.length; i++){ newArr[arr1[i]] = arr2[i]; }; console.log('first: '+newArr['first']+'; second: '+newArr['second']); }); 
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>