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.
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.
Comments