Object.keys 物件轉陣列

使用 Object.keys 物件轉陣列forEatch迴圈

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const roles = {
'001': {
name: '海綿寶寶',
expert: '吸收很快',
img:'https://cdn.pixabay.com/photo/2017/05/08/13/15/bird-2295431_1280.jpg'
},
'002': {
name: '耶夢',
expert: '任意門',
img:'https://cdn.pixabay.com/photo/2015/11/16/16/28/bird-1045954_1280.jpg'
},
'004': {
name: '超人',
expert: '英雄',
img:'https://cdn.pixabay.com/photo/2017/05/08/13/15/bird-2295431_1280.jpg'
},
};

const newKeysLists = []
Object.keys(roles).forEach((item)=>{
newKeysLists.push(roles[item])
})
console.log('newKeysLists:' ,newKeysLists)
'newKeysLists:'
[
{
name: '海綿寶寶',
expert: '吸收很快',
img:'https://cdn.pixabay.com/photo/2017/05/08/13/15/bird-2295431_1280.jpg'
},
{
name: '耶夢',
expert: '任意門',
img:'https://cdn.pixabay.com/photo/2015/11/16/16/28/bird-1045954_1280.jpg'
},
{
name: '超人',
expert: '英雄',
img:'https://cdn.pixabay.com/photo/2017/05/08/13/15/bird-2295431_1280.jpg'
},
]