将对象数组转换为Object的最佳方法是什么?

时间:2022-09-15 20:55:01

I need to convert the array of objects into object. I've done with the below logic. Is there is a best way to handle this?

我需要将对象数组转换为对象。我已经完成了以下逻辑。有没有最好的方法来处理这个?

Fiddler Version

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))

5 个解决方案

#1


7  

You can use forEach to iterate over an array, and you forgot to iterate over the nested array if there are multiple elements inside of an object.

您可以使用forEach迭代数组,如果对象内部有多个元素,则忘记迭代嵌套数组。

So, the code in question will not work for

因此,有问题的代码将不起作用

var arr = [{'a': 'b', 'c': 'd'}];

Demo

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>');

#2


3  

In latest browsers [ecmascript 5], we have reduce which returns an object

在最新的浏览器[ecmascript 5]中,我们有reduce返回一个对象

try below

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);

Fiddle here http://jsfiddle.net/pscytrgj/

小提琴这里http://jsfiddle.net/pscytrgj/

#3


3  

We use the best of functional programming in JavaScript:

我们在JavaScript中使用最好的函数式编程:

Using reduce we don't need to add temporary variables to the outer scope.

使用reduce我们不需要将临时变量添加到外部作用域。

for (var i in obj) is generally faster than Obj.keys(obj).forEach: https://jsperf.com/for-in-versus-object-keys-foreach

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>")

#4


2  

You can do it through JSON.stringify()

你可以通过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>")

#5


1  

How about Object.assign?

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))

#1


7  

You can use forEach to iterate over an array, and you forgot to iterate over the nested array if there are multiple elements inside of an object.

您可以使用forEach迭代数组,如果对象内部有多个元素,则忘记迭代嵌套数组。

So, the code in question will not work for

因此,有问题的代码将不起作用

var arr = [{'a': 'b', 'c': 'd'}];

Demo

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>');

#2


3  

In latest browsers [ecmascript 5], we have reduce which returns an object

在最新的浏览器[ecmascript 5]中,我们有reduce返回一个对象

try below

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);

Fiddle here http://jsfiddle.net/pscytrgj/

小提琴这里http://jsfiddle.net/pscytrgj/

#3


3  

We use the best of functional programming in JavaScript:

我们在JavaScript中使用最好的函数式编程:

Using reduce we don't need to add temporary variables to the outer scope.

使用reduce我们不需要将临时变量添加到外部作用域。

for (var i in obj) is generally faster than Obj.keys(obj).forEach: https://jsperf.com/for-in-versus-object-keys-foreach

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>")

#4


2  

You can do it through JSON.stringify()

你可以通过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>")

#5


1  

How about Object.assign?

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))