将 2 个数组映射到 1 个数组对象 [英] Map 2 array into 1 array object
本文介绍了将 2 个数组映射到 1 个数组对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 2 个单独的数组,但它们的长度相同.如何将它们合并成一个数组对象,以便以后轻松填充?
I have 2 separate array but both of them have same length. How to merge them together into an array object so that it's easy to populate later?
例如
[1,2,3,4,5]
['a','b','c','d','e']
我希望我可以有类似的东西
I expect I can have something like
[{'index':1,'value':'a'},{'index':2,'value':'b'}]
我试过了
$.each(a, function(i,x){
$.each(b, function(i,z){
c['index'] = x;
c['value'] = z;
});
});
但我只有 [{'index':'1','value':'a'}]
推荐答案
您可以使用 map()
用于迭代并生成新的数组
You can use map()
for iterate and generate the new array
var arr1 = [1, 2, 3, 4, 5],
arr2 = ['a', 'b', 'c', 'd', 'e'];
var res = arr1.map(function(v, i) {
return {
index: v,
value: arr2[i]
};
})
document.write('<pre>' + JSON.stringify(res, null, 3) + '</pre>');
这篇关于将 2 个数组映射到 1 个数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文