简体   繁体   English

如何返回属性与数组匹配的对象数组?

[英]How can I return an array of objects whose property matches an array?

I have an array like 我有一个像

array = [
  { name: "john", tag: ["tag1", "tag2"] },
  { name: "doe", tag: ["tag2"] },
  { name: "jane", tag: ["tag2", "tag3"] }
];

I want to get a new array of objects which contain both "tag2" and "tag3", but not only "tag2" or both "tag1" and "tag2". 我想获得一个包含“ tag2”和“ tag3”的对象的新数组,但不仅包含“ tag2”或“ tag1”和“ tag2”。

Result should be: 结果应为:

newArray = [{ name: "jane", tag: ["tag2", "tag3"] }];

I tried to do it using this process: 我尝试使用以下过程来做到这一点:

tags = ["tag2", "tag3"];
newArray = [];
tags.forEach(t => {
  array.forEach(data => {
    data.tag.forEach(item => {
      if (item === t) {
        newArray.push(data);
      }
    });
  });
});

But I get all the items instead. 但是我得到了所有物品。

If I understood you correctly, you want to search through your top-level array, to find all items whose tag property is an array exactly matching ['tag2', 'tag3'] . 如果我对您的理解正确,则希望搜索顶级数组,以查找其tag属性是与['tag2', 'tag3']完全匹配的数组的所有项目。

You can achieve this by filtering your array based on the above condition. 您可以通过根据上述条件过滤数组来实现。

Here's one approach: 这是一种方法:

 const array = [ { name: 'john', tag: ['tag1', 'tag2'] }, { name: 'doe', tag: ['tag2'] }, { name: 'jane', tag: ['tag2', 'tag3'] } ]; const tagsToMatchOn = ['tag2', 'tag3']; // find all elements who's tag property exactly matches // the above tags (in presence, not necessarily in order) const newArray = array.filter(item => ( item.tag.length === tagsToMatchOn.length && tagsToMatchOn.every(t => item.tag.includes(t)) )); console.log(newArray); 

If instead, you wanted to find all items whose tag property is an array including all of ['tag2', 'tag3' ] but can also include other tags, you can try something like this: 如果相反,您想查找其tag属性是一个包含['tag2', 'tag3' ]所有但也可以包含其他标签的数组的所有项目,则可以尝试如下操作:

 const array = [ { name: 'john', tag: ['tag1', 'tag2'] }, { name: 'doe', tag: ['tag2'] }, { name: 'jane', tag: ['tag2', 'tag3'] } ]; const tagsToMatchOn = ['tag2', 'tag3']; // find all elements who's tag property includes // all of the above tags but can also contain others const newArray = array.filter(item => tagsToMatchOn.every(t => item.tag.includes(t)) ); console.log(newArray); 

This mightn't be the most elegant solution but it does return what you want. 这可能不是最优雅的解决方案,但它确实会返回您想要的内容。

array = [{name:'john',
          tag: ['tag1','tag2'] 
         },
         {name:'doe',
          tag: ['tag2'] 
         },
         {name:'jane',
          tag: ['tag2','tag3'] 
         }
        ];

const newArray = [];
for (let index = 0; index < array.length; index++) {
    if(array[index].tag[0] === 'tag2' && array[index].tag[1] === 'tag3') {
        newArray.push(array[index])
    }
}

or if you want to be a bit more es6: 或者,如果您想更多地使用es6:

array.forEach(element => {
  if(element.tag[0] === 'tag2' && element.tag[1] === 'tag3') {
    newArray.push(element)
  }
});

You can do it like this 你可以这样

With the help of filter and every . 借助过滤器每一个

What basically i am doing here is first i am lopping through each element of arr (using filter). 基本上,我在这里要做的是首先要遍历arr的每个元素(使用过滤器)。 by using every i am checking does the tag property to element contains all the tags we require. 通过使用我正在检查的每一项,元素的tag属性是否包含我们需要的所有标签。 if it does than we include in our final output if not than we don't 如果超过,则将其包含在最终输出中;否则,我们将其包含在最终输出中

 let arr = [{name:'john', tag: ['tag1','tag2'] }, {name:'doe', tag: ['tag2'] }, {name:'jane', tag: ['tag2','tag3'] } ]; let tags = ['tag2','tag3']; let op = arr.filter(e=> tags.every(el=> e.tag.includes(el))); console.log(op); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用linq.js获取其子对象的属性与我的字符串数组匹配的所有对象? - How can get all objects whose sub object's property matches my string array using linq.js? 返回与另一个对象数组的属性匹配的对象数组的子集 - Return a subset of an array of objects that matches a property of an another array of objects Javascript从属性值匹配的对象数组返回数组 - Javascript return array from array of objects where property value matches LoDash从子数组中找到其属性与对象数组中的值匹配的对象 - LoDash find the object whose property matches the value from array of objects with children array 如何在具有对象的数组中查找匹配项并更改属性? - How to find matches in an array with objects and change property? 在对象数组中,如何在 React 中按属性返回所有对象? - In an array of Objects, how do I return all Objects by property in React? 如何求和对象数组中 javascript 数组的属性之和? - How can I sum the sum of a property of an javascript array in an array of objects? 如果属性与另一个数组匹配,则检索数组中的对象 - Retrieve objects in array if property matches another array 如果在数组的所有对象中将特定属性设置为 true,如何返回 true 布尔值? - How can I return true boolean if a specific property is set to true in all objects of an array? 如何使用对象更改数组中每个属性的值并返回它? - How do i change the value of each property in an array with objects and return it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM