javascript jquery将两个数组(key, value)关联成一个数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23908283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery associate two arrays (key, value) into one array
提问by tzortzik
How can I associate two arrays that contains keys and values into one array with key->value pairs?
如何将包含键和值的两个数组关联到一个具有键-> 值对的数组中?
In Mootools there is associate
function which does:
在 Mootools 中有一个associate
功能:
var animals = ['Cow', 'Pig', 'Dog', 'Cat'];
var sounds = ['Moo', 'Oink', 'Woof', 'Miao'];
sounds.associate(animals);
// returns {'Cow': 'Moo', 'Pig': 'Oink', 'Dog': 'Woof', 'Cat': 'Miao'}
Is there any similar function in JQuery to obtain the same result from those two arrays?
JQuery 中是否有类似的函数可以从这两个数组中获得相同的结果?
If not, how can I do it?
如果没有,我该怎么做?
采纳答案by Korikulum
JavaScript doesn't really have associative arrays, but you can use an object instead.
JavaScript 并没有真正的关联数组,但您可以使用对象来代替。
Array.prototype.associate = function (keys) {
var result = {};
this.forEach(function (el, i) {
result[keys[i]] = el;
});
return result;
};
var animals = ['Cow', 'Pig', 'Dog', 'Cat'];
var sounds = ['Moo', 'Oink', 'Woof', 'Miao'];
console.dir(sounds.associate(animals));
回答by kentaromiura
You can use Array.prototype.reduce
var keys = ['a', 'b', 'c'],
values = [1, 2, 3],
associated = keys.reduce(function (previous, key, index) {
previous[key] = values[index];
return previous
}, {})
console.log(associated) // Object {a: 1, b: 2, c: 3}
reduce is not supported natively on IE<9but you can safely use the Polyfill on the mdn sitewhich you can include using a conditional comment to target ie <9 only.
IE<9 本身不支持 reduce,但您可以安全地在 mdn 站点上使用Polyfill,您可以使用条件注释将其包含在内,以仅针对 IE <9。
If you want a reusable function is pretty straightforward to do:
如果你想要一个可重用的函数非常简单:
function associate(keys, values){
return keys.reduce(function (previous, key, index) {
previous[key] = values[index];
return previous
}, {})
}
回答by Nope
You could write your own similar to this:
你可以自己写类似的:
Array.prototype.associate = function(arr){
var index,
output = Object.create(null);
for(index = 0; index < this.length; index++){
output[arr[index]] = this[index];
}
return output;
};
Then you can use it as expected, similar to this:
然后你可以按预期使用它,类似于:
var animals = ['Cow', 'Pig', 'Dog', 'Cat'];
var sounds = ['Moo', 'Oink', 'Woof', 'Miao'];
var x = sounds.associate(animals);
The result in x
is {'Cow': 'Moo', 'Pig': 'Oink', 'Dog': 'Woof', 'Cat': 'Miao'}
结果x
是{'Cow': 'Moo', 'Pig': 'Oink', 'Dog': 'Woof', 'Cat': 'Miao'}
DEMO- Replicating Mootool's associate function
DEMO- 复制 Mootool 的关联功能
回答by Maneesh Singh
you can use in java scipt.
您可以在 java scipt 中使用。
Array.prototype.associate= function(){
var that = this;
var associated ={};
var len = that.length;
for(var i=0; i < len; i++){
associated[that[i]] = value[i];
}
return associated;
}
var animals = ['Cow', 'Pig', 'Dog', 'Cat'];
var sounds = ['Moo', 'Oink', 'Woof', 'Miao'];
console.log(animals.associate(sounds));
回答by MCL
Not jQuery, but simple enough to be achieved with pure JS (here's a fiddle):
不是 jQuery,但足够简单,可以用纯 JS 实现(这里有一个小提琴):
var animals = ['Cow', 'Pig', 'Dog', 'Cat'];
var sounds = ['Moo', 'Oink', 'Woof', 'Miao'];
var assoc = [];
for(var i=0; i<animals.length; i++) {
assoc[animals[i]] = sounds[i];
}
console.log(assoc);
prints:
印刷:
Cat: "Miao"
Cow: "Moo"
Dog: "Woof"
Pig: "Oink"
回答by Juan
If you can add a dependency like lodashto your project, then it's as easy as:
如果你可以在你的项目中添加一个像lodash这样的依赖项,那么它就像:
let result = _.zip([key1, key2], [value1, value2])
This will produce a new array of arrays:
这将产生一个新的数组数组:
[[key1, value1], [key2, value2]]
To the result, apply the lodash function fromPairs:
对于结果,应用 lodash 函数fromPairs:
let finalResult = _.fromPairs(resultArrayFromPreviousCode)
finalResult is now:
finalResult 现在是:
{ key1: value1, key2: value2 }
Hope that helps!
希望有帮助!
回答by Grant Miller
for...of Method
for...of 方法
You can also use a for...of
statement in conjunction with Array.prototype.entries()
to create an object using one array for keys and another for values:
您还可以for...of
结合使用语句Array.prototype.entries()
来创建一个对象,使用一个数组作为键,另一个用于值:
const array_combine = (keys, values) => {
const result = {};
for (const [index, key] of keys.entries()) {
result[key] = values[index];
}
return result;
};
const animals = ['Cow', 'Pig', 'Dog', 'Cat'];
const sounds = ['Moo', 'Oink', 'Woof', 'Miao'];
console.log(array_combine(animals, sounds));