Javascript: Mapping key:value into 2 separate key:values OR table with 2 columns

Viewed 24

I am currently coding a quiz that takes the input from users and attempts to convert input data into D3.js visualization like a word cloud.

Example inputs stored in an array:

const arr = ['one', 'two', 'one', 'one', 'two', 'three'];

const count = {};

arr.forEach(element => {
  count[element] = (count[element] || 0) + 1;
});

// ️ {one: 3, two: 2, three: 1}
console.log(count);

My question is, how do I map elements such as one,two,three etc to a new key as 'Word', and values 3, 2 and 1 to another key as 'Count'? In short, a table with 2 columns: Word and Count.

'Word' to display inside the word cloud visualization and 'Count' to control the size of the elements within visualization.

0 Answers
Related