Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to convert array of objects into Object?

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))
like image 263
Premkumar Jayaseelan Avatar asked Oct 14 '15 06:10

Premkumar Jayaseelan


People also ask

How do you reduce an array into an object?

The array reduce in JavaScript is a predefined method used to reduce an array to a single value by passing a callback function on each element of the array. It accepts a function executed on all the items of the specified array in the left-to-right sequence. The returned single value is stored in the accumulator.

Which method is used to convert nested arrays to object?

toString() method is used to convert: an array of numbers, strings, mixed arrays, arrays of objects, and nested arrays into strings.

How do you Destructure an array of objects?

To destructure an array in JavaScript, we use the square brackets [] to store the variable name which will be assigned to the name of the array storing the element.


1 Answers

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.

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>');
like image 116
Tushar Avatar answered Nov 03 '22 03:11

Tushar