Vijay Chouhan Vijay Chouhan - 6 months ago 13
Javascript Question

How to convert array of Objects into one Object in JavaScript?

I have array of Objects as

[Object { key="11", value="1100", $$hashKey="00X"}, Object { key="22", value="2200", $$hashKey="018"}]


How to convert it into by JavaScript

{"11":"1000", "22":"2200"}

Answer

You're probably looking for something like this:

// original
var arr = [ 
  {key : '11', value : '1100', $$hashKey : '00X' },
  {key : '22', value : '2200', $$hashKey : '018' }
];

//convert
var result = {};
for (var i=0; i<arr.length; i++) {
  result[arr[i].key] = arr[i].value;
}

//result
console.log(result);
Comments