When retrieving data from Firebase, I encountered an issue where I needed to convert the object into an array. Despite trying to use a foreach loop with map, I was unable to successfully achieve this conversion and ended up with an array of length 0.
0: {id: "1", name: "Fever"}
1: {id: "2", name: "Cough"}
2: {id: "3", name: "Headache"}
3: {id: "4", name: "Stomach Pain"}
I attempted the following method without success:
let result = [];
arr.forEach(item => {
let resObj = result.find(resObj => resObj.Name === item.Name);
resObj ? resObj.Count++ : result.push({'Name':item.Name, 'Value': item.Value, 'Count': 1});
});
console.log(result);
The desired output should look like this:
[
{id: "1", name: "Fever"},{id: "2", name: "Cough"},{id: "3", name: "Headache"},{id: "4", name: "Stomach Pain"}
]