Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two arrays as key/val

Tags:

javascript

How can you merge two arrays into key/value pairs please?

From this...

array1 = ['test1', 'test2'];
array2 = ['1', '2'];

To this...

array3 = ['test1':'1', 'test2':'2'];
like image 747
Alex Avatar asked Jan 18 '23 10:01

Alex


1 Answers

If you are using underscore.js http://documentcloud.github.com/underscore/#zip you can simply do:

var zipped = _.zip(array1,array2);
_(zipped).map(function(v){ return v[0] + ":" + v[1] });
like image 115
Erik Hinton Avatar answered Jan 21 '23 00:01

Erik Hinton