Convert array of single-property JavaScript objects to array of key/value pairs

maudulus

I have a collection of JavaScript dictionaries that look like

my_dictionary = [
    {"first_thing": "1"},
    {"second_thing": "2"}
]

, but which need to look like

my_dictionary = [
    {key: "first_thing", value: "1"},
    {key: "second_thing", value: "2"}
]

. Since there are so many of these dictionaries, I need a way to iterate through them and change all of the dictionaries so that they will have they key and value inside.

I have tried iterating through, and tried selecting them using something like my_dictionary[0].key as well as my_dictionary[0][0], which I hoped would work, but I suppose that isn’t the way to do it.

dandavis

Since all the transformation is happening in-element, i like using [].map() for this:

[{"first_thing": "1"}, {"second_thing":"2"}].map(function(o){
  var o2={};
  Object.keys(o).forEach(function(k){o2.key=k; o2.value=o[k];});
  return o2;
});

// == [{"key":"first_thing","value":"1"},{"key":"second_thing","value":"2"}]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

convert an array of objects to a single object with certain value pairs

JavaScript convert specified single objects into array

javascript Convert array of objects to single row of sum

How to convert an array of nested objects to an object with key, value pairs in JavaScript

Convert array of objects to comma separated key value pairs javascript

How to convert multiple objects to a single array and count the length of the objects in javascript?

Javascript convert objects to array

Function to convert single objects to an array?

convert arrays to a single array of objects

Javascript: Remove duplicates in array of objects using a single property

convert an array and an array of arrays into array of objects with key value pairs

Javascript: Convert array of objects with opening times to single string

Get property from objects to convert to simple array - javascript

Convert array of strings to array of objects in JSON property

How to convert an Array of Objects into a single Objects

convert array of array of objects into single array of objects in lodash

Converting array of javascript objects to key-value pairs of arrays based on property using ES6

Add property to an array of objects in JavaScript

Filter an array of objects by property JavaScript

Javascript Average of Array of Objects property

group an array of objects by a property javascript

Join Array of Objects by Property javascript

How do you convert an array of {"name":"myName","value":"myValue"} objects into an object of "myName":"myValue" pairs in JavaScript?

How to convert object containing key-value pairs to an array of objects in JavaScript

JavaScript: Convert array of objects into hashmap

Convert JSON into Array of JavaScript Objects

Convert string to array of objects in javascript

Javascript: Convert string to array of objects?

Javascript: Convert map to array of objects