我需要将对象数组转换为对象。我已完成以下逻辑。有没有最好的方法来解决这个问题?
var before = [{
"x": ["2015-10-14T01:59:59.999+05:30", "2015-10-14T03:59:59.998+05:30", "2015-10-14T05:59:59.997+05:30", "2015-10-14T07:59:59.996+05:30", "2015-10-14T09:59:59.995+05:30", "2015-10-14T11:59:59.994+05:30", "2015-10-14T13:59:59.993+05:30", "2015-10-14T15:59:59.992+05:30", "2015-10-14T17:59:59.991+05:30", "2015-10-14T19:59:59.990+05:30", "2015-10-14T21:59:59.989+05:30", "2015-10-14T23:59:59.988+05:30"]
}, {
"CleanCoal": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"]
}, {
"Middelings": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"]
}, {
"Prime": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"]
}, {
"SpiralProd": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"]
}];
var after = {};
for (var i = 0; i < before.length; i++) {
var keys = Object.keys(before[i]);
after[keys] = before[i][keys];
}
console.log(after)
document.writeln(JSON.stringify(after))
&#13;
答案 0 :(得分:7)
您可以使用forEach迭代数组,如果对象内部有多个元素,则忘记迭代嵌套数组。
因此,有问题的代码不适用于
var arr = [{'a': 'b', 'c': 'd'}];
<强>演示强>
var before = [{
"x": ["2015-10-14T01:59:59.999+05:30", "2015-10-14T03:59:59.998+05:30", "2015-10-14T05:59:59.997+05:30", "2015-10-14T07:59:59.996+05:30", "2015-10-14T09:59:59.995+05:30", "2015-10-14T11:59:59.994+05:30", "2015-10-14T13:59:59.993+05:30", "2015-10-14T15:59:59.992+05:30", "2015-10-14T17:59:59.991+05:30", "2015-10-14T19:59:59.990+05:30", "2015-10-14T21:59:59.989+05:30", "2015-10-14T23:59:59.988+05:30"]
}, {
"CleanCoal": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"]
}, {
"Middelings": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"]
}, {
"Prime": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"]
}, {
"SpiralProd": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"]
}];
var after = {};
before.forEach(function(obj) {
// obj here is the element of the array, i.e. object
// Looping over all the keys of the object
Object.keys(obj).forEach(function(key) {
// key here is the key of the object
after[key] = obj[key];
});
});
console.log(after);
document.writeln('<pre>' + JSON.stringify(after, 0, 2) + '</pre>');
答案 1 :(得分:3)
我们在JavaScript中使用最好的函数式编程:
使用reduce
我们不需要将临时变量添加到外部范围。
for (var i in obj)
通常比Obj.keys(obj).forEach
:https://jsperf.com/for-in-versus-object-keys-foreach
var before = [{
"x": ["2015-10-14T01:59:59.999+05:30", "2015-10-14T03:59:59.998+05:30", "2015-10-14T05:59:59.997+05:30", "2015-10-14T07:59:59.996+05:30", "2015-10-14T09:59:59.995+05:30", "2015-10-14T11:59:59.994+05:30", "2015-10-14T13:59:59.993+05:30", "2015-10-14T15:59:59.992+05:30", "2015-10-14T17:59:59.991+05:30", "2015-10-14T19:59:59.990+05:30", "2015-10-14T21:59:59.989+05:30", "2015-10-14T23:59:59.988+05:30"]
}, {
"CleanCoal": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"]
}, {
"Middelings": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"]
}, {
"Prime": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"]
}, {
"SpiralProd": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"]
}];
var after = before.reduce(function(dst, src){
// import every key: val in src into dst
// and keep doing it for every element in the array.
for (var key in src)
if ({}.hasOwnProperty.call(src, key))
dst[key] = src[key];
return dst;
}, {}); // start with an empty object to avoid modifying items in before array.
document.writeln("<pre>" + JSON.stringify(after, null, 4) + "</pre>")
&#13;
答案 2 :(得分:3)
在最新的浏览器[ecmascript 5]中,我们有reduce
返回一个对象
尝试以下
var before = [{"x":["2015-10-14T01:59:59.999+05:30","2015-10-14T03:59:59.998+05:30","2015-10-14T05:59:59.997+05:30","2015-10-14T07:59:59.996+05:30","2015-10-14T09:59:59.995+05:30","2015-10-14T11:59:59.994+05:30","2015-10-14T13:59:59.993+05:30","2015-10-14T15:59:59.992+05:30","2015-10-14T17:59:59.991+05:30","2015-10-14T19:59:59.990+05:30","2015-10-14T21:59:59.989+05:30","2015-10-14T23:59:59.988+05:30"]},{"CleanCoal":["0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00"]},{"Middelings":["0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00"]},{"Prime":["0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00"]},{"SpiralProd":["0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00"]}];
var object = before.reduce(function(o, v, i) {
o[i] = v;
return o;
}, {});
console.log(object);
&#13;
答案 3 :(得分:2)
您可以通过JSON.stringify()
var before = [{
"x": ["1", "2"],
y: {
a: 3
}
}, {
"CleanCoal": ["0.00", "0.00"],
b: 4
}, {
"Middelings": ["0.00", "0.00"]
}, {
"Prime": ["0.00", "0.00", "0.00"]
}, {
"SpiralProd": ["0.00", "0.00", "0.00"]
}];
var after = {};
var b = "",
i = -1;
while (before[++i]) {
var str = JSON.stringify(before[i]);
b += str.slice(1, str.length - 1);
if (before[i + 1]) b += ",";
}
after = JSON.parse("{" + b + "}");
console.log(after);
document.write("<pre>" + JSON.stringify(after, 0, 3) + "</pre>")
&#13;
答案 4 :(得分:1)
Object.assign怎么样?
var before = [{
"x": ["2015-10-14T01:59:59.999+05:30", "2015-10-14T03:59:59.998+05:30", "2015-10-14T05:59:59.997+05:30", "2015-10-14T07:59:59.996+05:30", "2015-10-14T09:59:59.995+05:30", "2015-10-14T11:59:59.994+05:30", "2015-10-14T13:59:59.993+05:30", "2015-10-14T15:59:59.992+05:30", "2015-10-14T17:59:59.991+05:30", "2015-10-14T19:59:59.990+05:30", "2015-10-14T21:59:59.989+05:30", "2015-10-14T23:59:59.988+05:30"]
}, {
"CleanCoal": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"]
}, {
"Middelings": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"]
}, {
"Prime": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"]
}, {
"SpiralProd": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"]
}];
var after = {};
before.map(function(i){
return Object.assign(after,i);
})
console.log(after)
document.writeln(JSON.stringify(after))