这是我的对象数组。因此,我想从中过滤出一组对象并制成另一个对象数组。
employees = [
{
{
name: "tom",
age : 22,
address: "moscow",
gender: "male"
},
{
name: "tom",
age : 25,
address: "manila",
gender: "male"
},
{
name: "tom",
age : 21,
address: "delhi",
gender: "female"
},
{
name: "tom",
age : 20,
address: "cydney",
gender: "female"
}
}
]
例如:想想我是否要过滤属于male
类别的员工并创建一个名为male_employees
的新数组,那么我该怎么做。
male_employees = [
{
{
name: "tom",
age : 22,
address: "moscow",
gender: "male"
},
{
name: "tom",
age : 25,
address: "manila",
gender: "male"
}
}
]
答案 0 :(得分:1)
正如某人评论的那样,只看一下.filter()函数,这里有很多关于如何使用它的示例。
您的情况如下:
male_employees = employees.filter(employee=>employee.gender==="male")
答案 1 :(得分:1)
尝试一下
male_employees_list = employees.filter(male_employee => male_employee.gender==="male")