Binson Eldhose
Binson Eldhose

Reputation: 749

How to convert array of key–value objects to array of objects with a single property?

I have an array of objects like this:

[
  { "key": "fruit", "value": "apple" },
  { "key": "color", "value": "red" },
  { "key": "location", "value": "garden" }
]

I need to convert it to the following format:

[
  { "fruit": "apple" },
  { "color": "red" },
  { "location": "garden" }
]

How can this be done using JavaScript?

Upvotes: 3

Views: 7448

Answers (5)

Oleksandr T.
Oleksandr T.

Reputation: 77522

You can use .map

var data = [
  {"key":"fruit","value":"apple"},
  {"key":"color","value":"red"},
  {"key":"location","value":"garden"}
];

var result = data.map(function (e) {
  var element = {};
  element[e.key] = e.value;
  
  return element;
});

console.log(result);

also if you use ES2015 you can do it like this

var result = data.map((e) => {
   return {[e.key]: e.value};
});

Example

Upvotes: 18

Paul S.
Paul S.

Reputation: 66404

Using an arrow function, with the data called arr

arr.map(e => {
    var o = {};
    o[e.key] = e.value;
    return o;
});

This generates a new Array and does not modify the original

It can be simplified down to one line as

arr.map(e => ({[e.key]: e.value}));

If you can't assume arrow function support yet, you would write this longhand

arr.map(function (e) {
    var o = {};
    o[e.key] = e.value;
    return o;
});

Upvotes: 6

In Javascript, obj.property and obj['property'] return same things. obj['property'] is more flexible because the key 'property' could be a string with some space :

obj['pro per ty'] // work
obj.pro per ty // not work

or

var a = 'property';
obj.a == obj.property // => false
obj[a] == obj.property // => true

So you could try that.

var data = [{"key":"fruit","value":"apple"},{"key":"color","value":"red"},{"key":"location","value":"garden"}]
var new_data = [];

var data_length = data.length; // just a little optimisation for-loop
for (var i = 0; i < data_length; i++) {
    var item = data[i]; // to have a vision close of foreach-loop (foreach item of collection)
    new_data[i] = {};
    new_data[i][item.key] = item.value;
}
console.log(new_data);
// [{"fruit":"apple"},{"color":"red"},{"location":"garden"}]

Upvotes: 1

Saurabh Tiwari
Saurabh Tiwari

Reputation: 5161

What you currently have is an array of object, each having two attributes, key and value. If you are not aware of map, you can always run a forEach loop on this array and rearrange the data. Try something like below:

  function() {
     var newArray = [];
     oldArray.forEach(function(x){
     var obj= {};
     obj[x.key] = x.value;
     newArray.push(obj);
    });
  console.log(newArray);
}

here oldArray is your original data

Upvotes: 0

Matthew Layton
Matthew Layton

Reputation: 42390

Using map (as suggested in other answers) or the following will do what you want...

var data = [{"key":"fruit","value":"apple"},{"key":"color","value":"red"},{"key":"location","value":"garden"}];
var obj = {};

for(var i = 0; i < data.length; i++) {
  obj[data[i]["key"]] = data[i]["value"];
}

Upvotes: 1

Related Questions