从键数组和值数组创建对象
Create an object from an array of keys and an array of values
我有两个数组:newParamArr
和 paramVal
。
newParamArr
数组中的示例值:[ "Name", "Age", "Email" ]
.
paramVal
数组中的示例值:[ "Jon", 15, "jon@gmail.com" ]
。
我需要创建一个 JavaScript 对象,将数组中的所有项目放在同一个对象中。例如 { [newParamArr[0]]: paramVal[0], [newParamArr[1]]: paramVal[1], ... }
.
在这种情况下,结果应该是 { Name: "Jon", "Age": 15, "Email": "jon@gmail.com" }
。
两个数组的长度始终相同,但数组的长度可以增加或减少。这意味着 newParamArr.length === paramVal.length
将永远成立。
None 以下帖子可能有助于回答我的问题:
Javascript Recursion for creating a JSON object
Recursively looping through an object to build a property list
使用 ECMAScript2015:
const obj = newParamArr.reduce((obj, value, index) => {
obj[value] = paramArr[index];
return obj;
}, {});
(编辑)之前误解了 OP 想要一个数组:
const arr = newParamArr.map((value, index) => ({[value]: paramArr[index]}))
使用循环:
var result = {};
for (var i = 0; i < newParamArr.length; i++) {
result[newParamArr[i]] = paramArr[i];
}
var keys = ['foo', 'bar', 'baz'];
var values = [11, 22, 33]
var result = {};
keys.forEach((key, i) => result[key] = values[i]);
console.log(result);
或者,您可以使用 Object.assign
result = Object.assign(...keys.map((k, i) => ({[k]: values[i]})))
或对象传播语法(ES2018):
result = keys.reduce((o, k, i) => ({...o, [k]: values[i]}), {})
或Object.fromEntries
(ES2019):
Object.fromEntries(keys.map((_, i) => [keys[i], values[i]]))
如果您使用的是 lodash,_.zipObject 正好适合这种类型的东西。
以下对我有用。
//test arrays
var newParamArr = [1, 2, 3, 4, 5];
var paramVal = ["one", "two", "three", "four", "five"];
//create an empty object to ensure it's the right type.
var obj = {};
//loop through the arrays using the first one's length since they're the same length
for(var i = 0; i < newParamArr.length; i++)
{
//set the keys and values
//avoid dot notation for the key in this case
//use square brackets to set the key to the value of the array element
obj[newParamArr[i]] = paramVal[i];
}
console.log(obj);
我知道这个问题已经有一年了,但这里有一个在线解决方案:
Object.assign( ...newParamArr.map( (v, i) => ( {[v]: paramVal[i]} ) ) );
您可以使用 Object.assign.apply()
将 {key:value} 对数组合并到您要创建的对象中:
Object.assign.apply({}, keys.map( (v, i) => ( {[v]: values[i]} ) ) )
一个可运行的片段:
var keys = ['foo', 'bar', 'baz'];
var values = [11, 22, 33]
var result = Object.assign.apply({}, keys.map( (v, i) => ( {[v]: values[i]} ) ) );
console.log(result); //returns {"foo": 11, "bar": 22, "baz": 33}
查看documentation了解更多
我在几个地方需要这个,所以我做了这个功能...
function zip(arr1,arr2,out={}){
arr1.map( (val,idx)=>{ out[val] = arr2[idx]; } );
return out;
}
console.log( zip( ["a","b","c"], [1,2,3] ) );
> {'a': 1, 'b': 2, 'c': 3}
这个适合我。
var keys = ['foo', 'bar', 'baz'];
var values = [11, 22, 33]
var result = {};
keys.forEach(function(key, i){result[key] = values[i]});
console.log(result);
Object.fromEntries 接受键数组、值元组和 return 压缩结果作为对象:
Object.fromEntries(keys.map((key, index)=> [key, values[index]]))
我有两个数组:newParamArr
和 paramVal
。
newParamArr
数组中的示例值:[ "Name", "Age", "Email" ]
.
paramVal
数组中的示例值:[ "Jon", 15, "jon@gmail.com" ]
。
我需要创建一个 JavaScript 对象,将数组中的所有项目放在同一个对象中。例如 { [newParamArr[0]]: paramVal[0], [newParamArr[1]]: paramVal[1], ... }
.
在这种情况下,结果应该是 { Name: "Jon", "Age": 15, "Email": "jon@gmail.com" }
。
两个数组的长度始终相同,但数组的长度可以增加或减少。这意味着 newParamArr.length === paramVal.length
将永远成立。
None 以下帖子可能有助于回答我的问题:
Javascript Recursion for creating a JSON object
Recursively looping through an object to build a property list
使用 ECMAScript2015:
const obj = newParamArr.reduce((obj, value, index) => {
obj[value] = paramArr[index];
return obj;
}, {});
(编辑)之前误解了 OP 想要一个数组:
const arr = newParamArr.map((value, index) => ({[value]: paramArr[index]}))
使用循环:
var result = {};
for (var i = 0; i < newParamArr.length; i++) {
result[newParamArr[i]] = paramArr[i];
}
var keys = ['foo', 'bar', 'baz'];
var values = [11, 22, 33]
var result = {};
keys.forEach((key, i) => result[key] = values[i]);
console.log(result);
或者,您可以使用 Object.assign
result = Object.assign(...keys.map((k, i) => ({[k]: values[i]})))
或对象传播语法(ES2018):
result = keys.reduce((o, k, i) => ({...o, [k]: values[i]}), {})
或Object.fromEntries
(ES2019):
Object.fromEntries(keys.map((_, i) => [keys[i], values[i]]))
如果您使用的是 lodash,_.zipObject 正好适合这种类型的东西。
以下对我有用。
//test arrays
var newParamArr = [1, 2, 3, 4, 5];
var paramVal = ["one", "two", "three", "four", "five"];
//create an empty object to ensure it's the right type.
var obj = {};
//loop through the arrays using the first one's length since they're the same length
for(var i = 0; i < newParamArr.length; i++)
{
//set the keys and values
//avoid dot notation for the key in this case
//use square brackets to set the key to the value of the array element
obj[newParamArr[i]] = paramVal[i];
}
console.log(obj);
我知道这个问题已经有一年了,但这里有一个在线解决方案:
Object.assign( ...newParamArr.map( (v, i) => ( {[v]: paramVal[i]} ) ) );
您可以使用 Object.assign.apply()
将 {key:value} 对数组合并到您要创建的对象中:
Object.assign.apply({}, keys.map( (v, i) => ( {[v]: values[i]} ) ) )
一个可运行的片段:
var keys = ['foo', 'bar', 'baz'];
var values = [11, 22, 33]
var result = Object.assign.apply({}, keys.map( (v, i) => ( {[v]: values[i]} ) ) );
console.log(result); //returns {"foo": 11, "bar": 22, "baz": 33}
查看documentation了解更多
我在几个地方需要这个,所以我做了这个功能...
function zip(arr1,arr2,out={}){
arr1.map( (val,idx)=>{ out[val] = arr2[idx]; } );
return out;
}
console.log( zip( ["a","b","c"], [1,2,3] ) );
> {'a': 1, 'b': 2, 'c': 3}
这个适合我。
var keys = ['foo', 'bar', 'baz'];
var values = [11, 22, 33]
var result = {};
keys.forEach(function(key, i){result[key] = values[i]});
console.log(result);
Object.fromEntries 接受键数组、值元组和 return 压缩结果作为对象:
Object.fromEntries(keys.map((key, index)=> [key, values[index]]))