[英]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.