将两个数组(键和值)合并到一个对象中 [英] merge two arrays (keys and values) into an object

查看:2483
本文介绍了将两个数组(键和值)合并到一个对象中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有常见的Javascript / Coffeescript特定的成语,我可以用来完成这个?主要是出于好奇。



我有两个数组,一个由所需的键组成,另一个由所需的值组成,我想将它合并到对象。

  keys = ['one','two','three'] 
values = ','b','c']


解决方案

p> var r = {},
i,
keys = ['one','two','three'],
values = ['a' 'b','c'];

for(i = 0; i r [keys [i]] = values [i]
}


Is there a common Javascript/Coffeescript-specific idiom I can use to accomplish this? Mainly out of curiosity.

I have two arrays, one consisting of the desired keys and the other one consisting of the desired values, and I want to merge this in to an object.

keys = ['one', 'two', 'three']
values = ['a', 'b', 'c']

解决方案

var r = {},
    i,
    keys = ['one', 'two', 'three'],
    values = ['a', 'b', 'c'];

for (i = 0; i < keys.length; i++) {
    r[keys[i]] = values[i];
}

这篇关于将两个数组(键和值)合并到一个对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆