简体   繁体   English

除非它包含具有特定值的另一个项目,否则如何从对象数组中获取具有属性的列表?

[英]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列表,但我只需要那些isPresenttrue的对象的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.

相关问题 如何检查包含另一个对象数组的对象数组是否具有属性 - How to check if array of objects that contains another array of object has property 如何从此数组中获取某些对象的值? - How do I get the value of certain objects from this array? 如何获取包含对象和数组的数组的值? - How to get a value of an array that contains objects and arrays? 从属性等于值的数组中获取项目 - Get Item from Array with Property equal to Value 如何从对象数组中获得一个从未出现在特定属性中的值 - How to get a value that never appear into specific property from the Array of objects 如何从 JavaScript 中的对象数组中获取属性值 - How to get a property value from an array of objects in JavaScript 从具有提供的属性的特定值的对象数组中获取索引的函数 - Function to get index from an array of objects having certain value of provided property 如果项的键(数组)包含另一个数组的每个值,如何提取? - How to extract item if its key (array) contains each and every value from another array? 当我在同一对象中拥有另一个属性的值时,从对象数组中获取一个Object属性 - Get a Object property from array of objects when I have the value of another property in the same object 将一个数组推入另一个数组,除非某个值已经“填充” - Pushing an array into another array unless a certain value is already 'filled'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM