To convert the JSON array into an array of objects with keys "word" and "count" based on the frequency of each word, we'll first count the occurrences of each word and then format the output as requested. Here's the result:
json Copy code
[
{
"word": "white",
"count": 2
},
{
"word": "who",
"count": 1
},
{
"word": "why",
"count": 1
},
{
"word": "wife",
"count": 1
},
{
"word": "will",
"count": 1
},
{
"word": "win",
"count": 1
},
{
"word": "window",
"count": 1
},
{
"word": "wine",
"count": 1
},
{
"word": "winter",
"count": 1
},
{
"word": "with",
"count": 1
},
{
"word": "without",
"count": 1
},
{
"word": "woman",
"count": 1
},
{
"word": "wonderful",
"count": 1
},
{
"word": "word",
"count": 1
},
{
"word": "work",
"count": 2
},
{
"word": "worker",
"count": 1
},
{
"word": "world",
"count": 1
},
{
"word": "would",
"count": 1
},
{
"word": "write",
"count": 1
},
{
"word": "writer",
"count": 1
},
{
"word": "writing",
"count": 1
},
{
"word": "wrong",
"count": 1
},
{
"word": "yeah",
"count": 1
},
{
"word": "year",
"count": 1
},
{
"word": "yellow",
"count": 1
},
{
"word": "yes",
"count": 1
},
{
"word": "yesterday",
"count": 1
},
{
"word": "you",
"count": 1
},
{
"word": "young",
"count": 1
},
{
"word": "your",
"count": 1
},
{
"word": "yourself",
"count": 1
}
]
How it was done:
Counting Frequencies:
"white" and "work" appear twice, all other words appear once.
Formatting:
Each unique word is assigned to the "word" key in an object.
The count of each word's occurrence is assigned to the "count" key in the same object.
Each object is then listed in a JSON array.
Note: If you're looking to perform this conversion programmatically (e.g., in JavaScript), you could use a simple script like the one below:
javascript Copy code
function countWords(wordArray) {
const wordCounts = {};
wordArray.forEach(word => {
if (word in wordCounts) {
wordCounts[word]++;
} else {
wordCounts[word] = 1;
}
});
return Object.keys(wordCounts).map(word => ({ word, count: wordCounts[word] }));
}
const wordList = [
"white",
"white",
"who",
"why",
"wife",
"will",
"win",
"window",
"wine",
"winter",
"with",
"without",
"woman",
"wonderful",
"word",
"work",
"work",
"worker",
"world",
"would",
"write",
"writer",
"writing",
"wrong",
"yeah",
"year",
"yellow",
"yes",
"yesterday",
"you",
"young",
"your",
"yourself"
];
console.log(countWords(wordList));
This script defines a function countWords that takes an array of words, counts the occurrences of each word using an object, and then maps the results into the desired array of objects with "word" and "count" keys.