I have an array of objects in JavaScript like this:
[
{A:10},
{B:20},
{C:30}
]
I'm trying to convert this into an array of objects that would appear like this (to work with d3.js):
[
{word: "A", weight: 10},
{word: "B", weight: 20},
{word: "C", weight: 30}
]
I'm able to get the initial array of objects by simply doing:
var wordData = [];
for (var i = 0; i < words.length; i++) {
if (wordData.hasOwnProperty(words[i])) {
wordData[words[i]] += 1;
} else {
wordData[words[i]] = 1;
}
}
This just sets up a simple hash, but doesn't add the keys before each one.
Edit
JSFiddle for simplicity.
{A, 10}doesn't look like a valid javascript, it should be{A:10}.weightvalue is the number of times that key (word) has occurred through iterating an array of words.