I've found a solution that gives the correct result when the word is at the same index, within each of the array elements, but in the case below, the term/word searched can be anywhere in the element (beginning or end).
array =
[
["A"], ["earth"], ["20"], ["tunnel"],
["house"], ["earth A"], ["$100"], ["house $100"]
]
Expected result:
result =
[
["A", 2], ["earth", 2], ["20", 1], ["tunnel", 1],
["house", 2], ["earth A", 1], ["$100", 2], ["house $100", 1],
]
Here's the attempt using the solution above:
array = [
["A"],
["earth"],
["20"],
["tunnel"],
["house"],
["earth A"],
["$100"],
["house $100"]
];
function count(array) {
return array.reduce((acc, arr) => {
for (const item of arr) {
acc[item] = acc[item] !== undefined ? acc[item] + 1 : 1
}
return acc
}, {})
}
console.log(count(array))
Appreciate your help!