[英]How to get list with of property from array of objects unless it contains another item with certain value?
I have an array of objects, and I need to get list with certain property from that array of objects.我有一个对象数组,我需要从该对象数组中获取具有某些属性的列表。 But i need that list to contain only those values where object was containing another property with certain element.
但我需要该列表仅包含 object 包含具有特定元素的另一个属性的那些值。 This is very confusing so i made an example.
这很令人困惑,所以我举了一个例子。 Let's say i have an array with objects.
假设我有一个包含对象的数组。
employees = [
{
n: 'case 1',
date: '2021-05-4',
id: '123',
user: [{name: 'Vlad', id: '1'}, {name: 'Misha', id: '2'}],
isPresent : true,
},
{
caseName: 'case 2',
date: '2021-05-4',
id: '124',
user: [{name: 'Alina', id: '3'}, {name: 'Alex', id: '4'}],
isPresent : true,
},
{
caseName: 'case 3',
date: '2021-05-4',
id: '126',
user: [],
isPresent : false,
},
]
And my task is to get a list of IDs
from array of objects, but i need ID
only from those objecrs which have isPresent
as true
.我的任务是从对象数组中获取
IDs
列表,但我只需要那些isPresent
为true
的对象的ID
。 So i need ['123', '124'].所以我需要['123','124']。
I could use a loops and conditions and so on.我可以使用循环和条件等等。 But i wondering is it possible to do with one line?
但我想知道是否可以用一根线做? Something like this:
像这样的东西:
employees.filter(item => { return item.isPresent === true }))
But i need only IDs
not whole objects.但我只需要
IDs
而不是整个对象。
1) You can filter the elements with condition item.isPresent === true
and then map over it to get the final result as: 1)您可以使用条件
item.isPresent === true
过滤元素,然后对其进行 map 过滤以获得最终结果:
employees
.filter((item) => item.isPresent === true)
.map((o) => o.id);
or you can also do as:或者你也可以这样做:
employees.filter((item) => item.isPresent).map((o) => o.id)
const employees = [{ n: 'case 1', date: '2021-05-4', id: '123', user: [{ name: 'Vlad', id: '1' }, { name: 'Misha', id: '2' }], isPresent: true, }, { caseName: 'case 2', date: '2021-05-4', id: '124', user: [{ name: 'Alina', id: '3' }, { name: 'Alex', id: '4' }], isPresent: true, }, { caseName: 'case 3', date: '2021-05-4', id: '126', user: [], isPresent: false, }, ] const result = employees.filter((item) => item.isPresent === true).map((o) => o.id); console.log(result);
2) You can also achieve the same result using reduce
as: 2)您也可以使用
reduce
来实现相同的结果:
employees.reduce((acc, curr) => {
curr.isPresent && acc.push(curr.id);
return acc;
}, []);
const employees = [ { n: "case 1", date: "2021-05-4", id: "123", user: [ { name: "Vlad", id: "1" }, { name: "Misha", id: "2" }, ], isPresent: true, }, { caseName: "case 2", date: "2021-05-4", id: "124", user: [ { name: "Alina", id: "3" }, { name: "Alex", id: "4" }, ], isPresent: true, }, { caseName: "case 3", date: "2021-05-4", id: "126", user: [], isPresent: false, }, ]; const result = employees.reduce((acc, curr) => { curr.isPresent && acc.push(curr.id); return acc; }, []); console.log(result);
You can use something like this你可以使用这样的东西
employees.filter((item) => item.isPresent).map((obj) => obj.id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.