close

轉自 https://stackoverflow.com/questions/37224085/how-to-remove-key-from-array-of-object

--

Q:

I have Array objects like below , How to convert this format into Array of objects and remove key.

{
  "9417702107": {
    "name": "Sunny",
    "phone": "9417702107",
    "exists": true
  },
  "8826565107": {
    "name": "Gurwinder",
    "phone": "8826565107",
    "exists": true
  }
}

How to convert this into below format using javascript:

[{
  "name": "Sunny",
  "phone": "9417702107",
  "exists": true
}, {
  "name": "Gurwinder",
  "phone": "8826565107",
  "exists": true
}]

A:

Use a simple loop:

 

array = [];
for (var key in obj) {
    array.push(obj[key]);
}

 

As in the other answer, there's no guarantee that the elements of the array will be in the same order as in the object.

--

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 dizzy03 的頭像
    dizzy03

    碎碎念

    dizzy03 發表在 痞客邦 留言(0) 人氣()