3

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?

1

5 Answers 5

18

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

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

Comments

6

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

4 Comments

@TrueBlueAussie Nope, arrow functions: developer.mozilla.org/en/docs/Web/JavaScript/Reference/…
Arrow functions are ES6, so make sure that your platform supports ES6.
@AlexK. according to MDN it should in Safari and IE9 and greater. I haven't tested personally though. If you have to support <IE9, may God have mercy on your soul ;)
@RoryMcCrossan As of today (2016-01-12), all versions of IE below 11 have reached end of life
1

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"];
}

Comments

1

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"}]

1 Comment

Add some explanation to your answer that will be helpful.
0

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

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.