返回数组 array中所有元素id都等于 8 的元素
- 已知数组array:
let array = [
{
id: 1,
name: "服务A"
},
{
id: 1,
name: "服务A"
},
{
id: 4,
name: "服务B"
},
{
id: 8,
name: "服务D"
},
{
id: 8,
name: "服务D"
},
{
id: 8,
name: "服务D"
},
{
id: 8,
name: "服务D"
}
]
- 筛选出id值为8的item,并返回新数组items
let items = array.filter((item) => {
return item.id == 8
})
console.log(items)
- 最终结果
输出内容:[{id: 8,name: "服务D"}, {id: 8,name: "服务D"}, {id: 8,name: "服务D"}, {id: 8,name: "服务D"}]