I have an array of Objects:
我有一个对象数组:
[Object { key="11", value="1100", $$hashKey="00X"}, Object { key="22", value="2200", $$hashKey="018"}]
How do I convert it into the following by JavaScript?
如何通过JavaScript将其转换为以下内容?
{"11":"1000", "22":"2200"}
9 个解决方案
#1
19
You're probably looking for something like this:
你可能正在寻找这样的东西:
// original
var arr = [
{key : '11', value : '1100', $$hashKey : '00X' },
{key : '22', value : '2200', $$hashKey : '018' }
];
//convert
var result = {};
for (var i=0; i<arr.length; i++) {
result[arr[i].key] = arr[i].value;
}
//result
console.log(result);
#2
33
I like the functional approach to achieve this task:
我喜欢实现此任务的功能方法:
var arr = [{ key:"11", value:"1100" }, { key:"22", value:"2200" }];
var result = arr.reduce(function(obj,item){
obj[item.key] = item.value;
return obj;
}, {});
Note: Last {}
is the initial obj
value for reduce function, if you won't provide the initial value the first arr
element will be used (which is probably undesirable).
注意:Last {}是reduce函数的初始obj值,如果不提供初始值,则将使用第一个arr元素(这可能是不合需要的)。
#3
24
Tiny ES6 solution can look like:
微小的ES6解决方案可能如下所示:
var arr = [{key:"11", value:"1100"},{key:"22", value:"2200"}];
var object = arr.reduce(
(obj, item) => Object.assign(obj, {[item.key]: item.value}) ,{});
Also, if you use object spread, than it can look like:
此外,如果您使用对象传播,它可能看起来像:
var object = arr.reduce((obj, item) => ({...obj, {[item.key]: item.value}}) ,{});
One more solution that is 99% faster is(tested on jsperf):
另一个快99%的解决方案是(在jsperf上测试):
var object = arr.reduce((obj, item) => (obj[item.key] = item.value, obj) ,{});
Here we benefit from comma operator, it evaluates all expression before comma and returns a last one(after last comma). So we don't copy obj
each time, rather assigning new property to it.
这里我们受益于逗号运算符,它在逗号之前计算所有表达式并返回最后一个(在最后一个逗号之后)。所以我们不是每次都复制obj,而是为它分配新的属性。
#4
4
Trying to fix this answer, this should do it:
试着解决这个问题,应该这样做:
var array = [
{key:'k1',value:'v1'},
{key:'k2',value:'v2'},
{key:'k3',value:'v3'}
];
var mapped = array .map(item => ({ [item.key]: item.value }) );
var newObj = Object.assign({}, ...mapped );
console.log(newObj );
One-liner:
var newObj = Object.assign({}, ...(array.map(item => ({ [item.key]: item.value }) )));
#6
2
Using Underscore.js:
var myArray = [
Object { key="11", value="1100", $$hashKey="00X"},
Object { key="22", value="2200", $$hashKey="018"}
];
var myObj = _.object(_.pluck(myArray, 'key'), _.pluck(myArray, 'value'));
#7
0
Here you go:
干得好:
var arr = [{ key: "11", value: "1100" }, { key: "22", value: "2200" }];
var result = {};
for (var i=0, len=arr.length; i < len; i++) {
result[arr[i].key] = arr[i].value;
}
console.log(result); // {11: "1000", 22: "2200"}
#8
0
Here's how to dynamically accept the above as a string and interpolate it into an object:
以下是如何动态接受上面的字符串并将其插入到对象中:
var stringObject = '[Object { key="11", value="1100", $$hashKey="00X"}, Object { key="22", value="2200", $$hashKey="018"}]';
function interpolateStringObject(stringObject) {
var jsObj = {};
var processedObj = stringObject.split("[Object { ");
processedObj = processedObj[1].split("},");
$.each(processedObj, function (i, v) {
jsObj[v.split("key=")[1].split(",")[0]] = v.split("value=")[1].split(",")[0].replace(/\"/g,'');
});
return jsObj
}
var t = interpolateStringObject(stringObject); //t is the object you want
#9
0
// original
var arr = [{
key: '11',
value: '1100',
$$hashKey: '00X'
},
{
key: '22',
value: '2200',
$$hashKey: '018'
}
];
// My solution
var obj = {};
for (let i = 0; i < arr.length; i++) {
obj[arr[i].key] = arr[i].value;
}
console.log(obj)
#1
19
You're probably looking for something like this:
你可能正在寻找这样的东西:
// original
var arr = [
{key : '11', value : '1100', $$hashKey : '00X' },
{key : '22', value : '2200', $$hashKey : '018' }
];
//convert
var result = {};
for (var i=0; i<arr.length; i++) {
result[arr[i].key] = arr[i].value;
}
//result
console.log(result);
#2
33
I like the functional approach to achieve this task:
我喜欢实现此任务的功能方法:
var arr = [{ key:"11", value:"1100" }, { key:"22", value:"2200" }];
var result = arr.reduce(function(obj,item){
obj[item.key] = item.value;
return obj;
}, {});
Note: Last {}
is the initial obj
value for reduce function, if you won't provide the initial value the first arr
element will be used (which is probably undesirable).
注意:Last {}是reduce函数的初始obj值,如果不提供初始值,则将使用第一个arr元素(这可能是不合需要的)。
#3
24
Tiny ES6 solution can look like:
微小的ES6解决方案可能如下所示:
var arr = [{key:"11", value:"1100"},{key:"22", value:"2200"}];
var object = arr.reduce(
(obj, item) => Object.assign(obj, {[item.key]: item.value}) ,{});
Also, if you use object spread, than it can look like:
此外,如果您使用对象传播,它可能看起来像:
var object = arr.reduce((obj, item) => ({...obj, {[item.key]: item.value}}) ,{});
One more solution that is 99% faster is(tested on jsperf):
另一个快99%的解决方案是(在jsperf上测试):
var object = arr.reduce((obj, item) => (obj[item.key] = item.value, obj) ,{});
Here we benefit from comma operator, it evaluates all expression before comma and returns a last one(after last comma). So we don't copy obj
each time, rather assigning new property to it.
这里我们受益于逗号运算符,它在逗号之前计算所有表达式并返回最后一个(在最后一个逗号之后)。所以我们不是每次都复制obj,而是为它分配新的属性。
#4
4
Trying to fix this answer, this should do it:
试着解决这个问题,应该这样做:
var array = [
{key:'k1',value:'v1'},
{key:'k2',value:'v2'},
{key:'k3',value:'v3'}
];
var mapped = array .map(item => ({ [item.key]: item.value }) );
var newObj = Object.assign({}, ...mapped );
console.log(newObj );
One-liner:
var newObj = Object.assign({}, ...(array.map(item => ({ [item.key]: item.value }) )));
#5
#6
2
Using Underscore.js:
var myArray = [
Object { key="11", value="1100", $$hashKey="00X"},
Object { key="22", value="2200", $$hashKey="018"}
];
var myObj = _.object(_.pluck(myArray, 'key'), _.pluck(myArray, 'value'));
#7
0
Here you go:
干得好:
var arr = [{ key: "11", value: "1100" }, { key: "22", value: "2200" }];
var result = {};
for (var i=0, len=arr.length; i < len; i++) {
result[arr[i].key] = arr[i].value;
}
console.log(result); // {11: "1000", 22: "2200"}
#8
0
Here's how to dynamically accept the above as a string and interpolate it into an object:
以下是如何动态接受上面的字符串并将其插入到对象中:
var stringObject = '[Object { key="11", value="1100", $$hashKey="00X"}, Object { key="22", value="2200", $$hashKey="018"}]';
function interpolateStringObject(stringObject) {
var jsObj = {};
var processedObj = stringObject.split("[Object { ");
processedObj = processedObj[1].split("},");
$.each(processedObj, function (i, v) {
jsObj[v.split("key=")[1].split(",")[0]] = v.split("value=")[1].split(",")[0].replace(/\"/g,'');
});
return jsObj
}
var t = interpolateStringObject(stringObject); //t is the object you want
#9
0
// original
var arr = [{
key: '11',
value: '1100',
$$hashKey: '00X'
},
{
key: '22',
value: '2200',
$$hashKey: '018'
}
];
// My solution
var obj = {};
for (let i = 0; i < arr.length; i++) {
obj[arr[i].key] = arr[i].value;
}
console.log(obj)