how can i convert object into an array in javascript?

vivek manavadariya :

I got object from firebase like this way and i need to convert into an array i used a foreach with map but i didn't get success. i got 0 length of an array.

0: {id: "1", name: "Fever"}
1: {id: "2", name: "Cough"}
2: {id: "3", name: "Headache"}
3: {id: "4", name: "Stomach Pain"}

i used below method but it's not working

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);

i need the output like this

[
{id: "1", name: "Fever"},{id: "2", name: "Cough"},{id: "3", name: "Headache"},{id: "4", name: "Stomach Pain"}
]
sarvagya :

You can do it like this:

let obj = {
  0: {
    id: "1",
    name: "Fever"
  },
  1: {
    id: "2",
    name: "Cough"
  },
  2: {
    id: "3",
    name: "Headache"
  },
  3: {
    id: "4",
    name: "Stomach Pain"
  }
};
let arr = [];
for (let key in obj) {
  arr.push(obj[key]);
}
console.log(arr);

The for ... in loop iterates over each obj key and push all the values of the obj in the arr.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=319562&siteId=1