score:5

Accepted answer

you can use object.keys and map:

var obj = {
 word1 : 1,
 word2 : 1,
 word3 : 2,
 word4 : 3
}

var array = object.keys(obj).map((key) => ({
  word: key,
  count: obj[key]
}));

console.log(array);

score:0

you can use reduce() on the object.keys for this:

let o = {
  word1: 1,
  word2: 1,
  word3: 2,
  word4: 3
};

let arr = object.keys(o).reduce((a, b) => a.concat({
  word: b,
  count: o[b]
}), []);

console.log(arr);

score:0

this should work.

const start = {
  word1 : 1,
  word2 : 1,
  word3 : 2,
  word4 : 3
};

const finish = object.keys(start).map(k => { word: k, count: start[k] });

score:0

you should push an object for each key in the object:

var result = [];
for (var key in p) {
  if (p.hasownproperty(key)) {
    result.push({
      word: key,
      count: p[key]
    });
  }
}

score:0

you can use object.keys() to iterate through each key in the object. use map() or foreach() to get the desired result.

let o = {
  word1: 1,
  word2: 1,
  word3: 2,
  word4: 3
};

var result = [];

object.keys(o).foreach(function(x){
  result.push({word: x, count : o[x]});
});

console.log("result with foreach :" ,result);

var resultwithmap = object.keys(o).map( x =>
  ({ word : x, count : o[x] }) );

console.log("result with map :" , resultwithmap);


Related Query

More Query from same tag