admin管理员组文章数量:1355642
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'];
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'];
Share
Improve this question
edited Oct 27, 2011 at 13:59
Alex
asked Oct 27, 2011 at 13:48
AlexAlex
1812 silver badges9 bronze badges
2
-
2
The syntax of
array3
you provided isn't "key-value" syntax. – duri Commented Oct 27, 2011 at 13:51 -
1
I think for your result you mean
array3 = {'test1':'1', 'test2':'2'};
, don't you? If so, then the answer with the phpjs array_bine will do just fine. – Jonathan M Commented Oct 27, 2011 at 13:53
2 Answers
Reset to default 5If you are using underscore.js http://documentcloud.github./underscore/#zip you can simply do:
var zipped = _.zip(array1,array2);
_(zipped).map(function(v){ return v[0] + ":" + v[1] });
See http://phpjs/functions/array_bine:307
EDIT: Looking at your question again, you might be after something more like this:
function mergeArrays(arr1, arr2) {
var l = Math.min(arr1.length,arr2.length),
ret = [],
i;
for( i=0; i<l; i++) {
ret.push(arr1[i]+":"+arr2[i]);
}
return ret;
}
本文标签: javascriptHow to merge two arrays as keyvalStack Overflow
版权声明:本文标题:javascript - How to merge two arrays as keyval - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743944032a2566073.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论