Premkumar
Premkumar

Reputation: 701

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

Upvotes: 5

Views: 2279

Answers (5)

Sherali Turdiyev
Sherali Turdiyev

Reputation: 1743

You can do it through 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>")

Upvotes: 2

homam
homam

Reputation: 1975

We use the best of functional programming in JavaScript:

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

for (var i in obj) is generally faster than 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>")

Upvotes: 3

Tushar
Tushar

Reputation: 87203

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

Upvotes: 8

Anand G
Anand G

Reputation: 3200

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

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/

Upvotes: 3

Bhargav Ponnapalli
Bhargav Ponnapalli

Reputation: 9412

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

Upvotes: 1

Related Questions