I have an object that consists in the number of occurrences for different words. It looks like this :
{
Word1 : 1,
Word2 : 1,
Word3 : 2,
Word4 : 3
}
I would like to have an array that looks like this :
[
{word:Word1, count:1},
{word:Word2, count:1},
{word:Word3, count:2},
{word:Word4, count:3},
]
I looked a bit around and found this code that loops through the object and get the values I want :
for (var key in p) {
if (p.hasOwnProperty(key)) {
console.log(key + " -> " + p[key]);
}
}
I tried to create an empty array and to use push to get the intended values in it, but I don't seem to get the hang of it. I feel like something along those lines should get me the result I want :
for (var key in p) {
if (p.hasOwnProperty(key)) {
//Here is where the code should go, something like :
myNewObject[i].word = key;
myNewObject[i].count = p[key];
}
}
word4?