我正在尝试获取仅具有兴趣爱好且角色为'A'类型的员工的列表。
我尝试使用以下查询,但没有成功。
我在做什么错了?
// Get all the employees with active hobbies and have a role of 'A'
var employees = people.find(item => item.key === 'Employees').employees.map(emp => emp.hobbies).filter(hobby => hobby.filter(h => h.active === true && h.roles.includes('A')));
console.log(employees);
<script>
var people = [{
key: 'Employees',
employees: [{
name: 'joe',
age: 20,
hobbies: [{
'active': true,
name: 'skating',
roles: ['C', 'A']
}]
},
{
name: 'amy',
age: 32,
hobbies: [{
'active': true,
name: 'surfing',
roles: ['A']
}]
},
{
name: 'kate',
age: 34,
hobbies: [{
'active': true,
name: 'running',
roles: ['C']
}, {
name: 'Chess',
active: false,
roles: ['C', 'A']
}]
}
]
}];
</script>
更新
当我向阵列中添加更多具有新爱好的员工时,被接受的答案无法产生正确的输出。为什么会这样?
var people = [{
key: 'Employees',
employees: [{
name: 'Steve',
age: 50,
hobbies: [{
active: true,
name: 'skating',
roles: ['C', 'A']
},
{
active: false,
name: 'skating',
roles: ['C', 'A']
},
{
active: true,
name: 'snooker',
roles: ['C', 'A']
},
{
active: true,
name: 'darts',
roles: ['C', 'A']
}
]
},{
name: 'joe',
age: 20,
hobbies: [{
active: true,
name: 'skating',
roles: ['C', 'A']
}
]
}, {
name: 'amy',
age: 32,
hobbies: [{
'active': true,
name: 'surfing',
roles: ['A']
}
]
}, {
name: 'kate',
age: 34,
hobbies: [{
active: true,
name: 'running',
roles: ['C']
}, {
name: 'Chess',
active: false,
roles: ['C', 'A']
}
]
}
]
}
];
var employees = people.find(item => item.key === 'Employees').employees.filter(employee => employee.hobbies.every(h => h.active && h.roles.includes('A')));
答案 0 :(得分:1)
如果您这样做:
.map(emp => emp.hobbies).filter(hobby =>
您将每个员工映射到其兴趣爱好,这些兴趣爱好将填充到2D
数组中:
[[ { active: true }, { active: false } ], [/*...*/]]
因此,hobby
不是一个爱好,而是一系列爱好。
你说
我正在尝试获取员工列表
......这实际上意味着您不想.map
参加爱好,而是.filter
名员工并检查ever
爱好是否满足某些规则:
const employees = people.find(({ key }) => key === "Employees").employees;
const isActive = hobby => hobby.active && hobby.roles.includes("A");
const result = employees.filter(emp => emp.hobbies.every(isActive));
答案 1 :(得分:1)
将员工映射到他们的兴趣爱好时出了错:这将使您的最终结果由兴趣爱好而不是员工组成。
您需要坚持员工级别:
var people = [{key: 'Employees',employees: [{ name: 'joe', age: 20, hobbies: [{'active': true, name: 'skating', roles: ['C', 'A'] }] },{ name: 'amy', age: 32, hobbies: [{'active': true, name: 'surfing', roles: ['A'] }] }, { name: 'kate', age: 34, hobbies: [{'active': true, name: 'running', roles: ['C']}, {name: 'Chess', active: false, roles: ['C','A']}] }]}];
var employees = people.find(item => item.key === 'Employees').employees
.filter(employee => employee.hobbies.every(h => h.active && h.roles.includes('A')));
console.log(employees);
在表达式中,无需将布尔属性与true
进行比较。只需使用属性(在这种情况下为active
)即可。
如果要求员工至少 个这样的嗜好,而不是要求所有个嗜好,请使用.some
代替.every
符合条件。
答案 2 :(得分:1)
.map(emp => emp.hobbies)
返回兴趣爱好的数组,因此employees
的值将是兴趣爱好的过滤列表,而不是具有这些兴趣爱好的员工。您需要过滤员工,而不是映射他们。
// Get all the employees with active hobbies and have a role of 'A'
var employees = people.find(item => item.key === 'Employees').employees.filter(emp =>
emp.hobbies.every(h => h.active && h.roles.includes('A')));
console.log(employees);
<script>
var people = [{
key: 'Employees',
employees: [{
name: 'joe',
age: 20,
hobbies: [{
'active': true,
name: 'skating',
roles: ['C', 'A']
}]
},
{
name: 'amy',
age: 32,
hobbies: [{
'active': true,
name: 'surfing',
roles: ['A']
}]
},
{
name: 'kate',
age: 34,
hobbies: [{
'active': true,
name: 'running',
roles: ['C']
}, {
name: 'Chess',
active: false,
roles: ['C', 'A']
}]
}
]
}];
</script>
答案 3 :(得分:1)
您只需致电Array.prototype.filter
即可检查您提到的条件:
person.hobbies.every(y => y.active) && person.hobbies.every(z => z.roles.includes('A'))
var people = [{
key: 'Employees',
employees: [{
name: 'joe',
age: 20,
hobbies: [{
'active': true,
name: 'skating',
roles: ['C', 'A']
}]
},
{
name: 'amy',
age: 32,
hobbies: [{
'active': true,
name: 'surfing',
roles: ['A']
}]
},
{
name: 'kate',
age: 34,
hobbies: [{
'active': true,
name: 'running',
roles: ['C']
}, {
name: 'Chess',
active: false,
roles: ['C', 'A']
}]
}
]
}];
let employees = people[0].employees.filter(x =>
x.hobbies.every(y => y.active) && x.hobbies.every(z => z.roles.includes('A'))
)
console.log(employees);
答案 4 :(得分:0)
您可以使用map
和filter
:
var people = [{ key: 'Employees', employees: [{ name: 'joe', age: 20, hobbies: [{ 'active': true, name: 'skating', roles: ['C', 'A'] }] }, { name: 'amy', age: 32, hobbies: [{ 'active': true, name: 'surfing', roles: ['A'] }] }, { name: 'kate', age: 34, hobbies: [{ 'active': true, name: 'running', roles: ['C'] }, { name: 'Chess', active: false, roles: ['C', 'A'] }] } ] }];
const result = people.filter(x => x.key == 'Employees')
.map(({employees}) =>
employees.filter(x => x.hobbies.some(y => y.active && y.roles.includes('A'))))
console.log(result)
您还可以使用reduce
和filter
:
var people = [{ key: 'Employees', employees: [{ name: 'joe', age: 20, hobbies: [{ 'active': true, name: 'skating', roles: ['C', 'A'] }] }, { name: 'amy', age: 32, hobbies: [{ 'active': true, name: 'surfing', roles: ['A'] }] }, { name: 'kate', age: 34, hobbies: [{ 'active': true, name: 'running', roles: ['C'] }, { name: 'Chess', active: false, roles: ['C', 'A'] }] } ] }];
const result = people.filter(x => x.key == 'Employees').reduce((r,{employees}) =>
{
r.push(employees.filter(x =>
x.hobbies.some(y => y.active && y.roles.includes('A'))))
return r
}, [])
console.log(result)