5

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

2
  • your version is not true, if {a:1, b:4, ....} object's key will be more than one. Commented Oct 14, 2015 at 6:19
  • Your approach only works because the inner arrays each have only one key. With more than one it would fail. You need a second loop. Commented Oct 14, 2015 at 6:19

5 Answers 5

8

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the response. I also need to change the date format from string to date type. I've updated the code snippet. How efficiently I can convert?
@PremkumarJayaseelan Please ask a different question for date format change
Hi, brother, Thanks for JSON.stringify(after, 0, 2). I didn't know. What do you think about my answer. Can you advise me(is it good way or not)?
3

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/

2 Comments

reduce doesn't necessarily return an object, it returns whatever the callback returns as an accumulator (which can be any value). MDN provides a polyfill for reduce so older browsers are accommodated too.
@RobG Yeah, that's true! kinda learning for me as well :) Thanks!
3

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

1 Comment

Code-only answers are not optimal. You should explain why your code meets the OPs requirements.
2

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.