简体   繁体   English

针对另一个对象过滤对象数组

[英]Filtering an array of objects against another object

I have an array of objects that i need to filter by a certain criteria. 我有一些对象,需要按特定条件过滤。 I'm having trouble figuring out the logic for the if statement within the for loop. 我在弄清楚for循环中if语句的逻辑时遇到麻烦。 I've attached a code snippet where you can adjust the criteria and see the problem I'm trying to solve. 我随附了一个代码段,您可以在其中调整条件并查看我要解决的问题。 Any thoughts or advice are greatly appreciated, thanks! 任何想法或建议都将不胜感激,谢谢!

With the following criteria I should only end up with 1 found item in my foundItems array: 根据以下条件,我只能在foundItems数组中得到1个找到的项目:

const criteria = {
    title: 'title',
    character: 'Z',
    type: ['second'],
};

This should(and does) return all three items: 这应该(并且确实)返回所有三个项目:

const criteria = {
    title: 'title',
    character: '',
    type: [],
};

This should return the first two items: 这应该返回前两个项目:

const criteria = {
    title: 'title',
    character: 'R',
    type: [],
};

This should return all three items: 这应该返回所有三个项目:

const criteria = {
    title: '',
    character: '',
    type: ['first','second'],
};

 const data = [ { label: { title: 'A title', }, character: 'R', type: 'first', }, { label: { title: 'Another title', }, character: 'R', type: 'second', }, { label: { title: 'A more interesting title', }, character: 'Z', type: 'second', }, ]; const criteria = { title: 'title', character: 'Z', type: ['second'], }; const createRegEx = (value) => { const regex = value .split(' ') .filter(Boolean) .map((word) => `(?=^.*${word})`) .join(''); return new RegExp(regex, 'i'); } const foundItems = []; for (let i = 0; i < data.length; i++) { const item = data[i]; if ( item.label.title.match(createRegEx(criteria.title)) || item.character === criteria.character || criteria.type.includes(item.type) ) { foundItems[foundItems.length] = item; } } console.log(foundItems); 

This demonstrates what I believe to be your intent. 这表明我相信是您的意图。 Please let me know if it needs to be corrected. 请让我知道是否需要更正。 I took some liberty to simplify the code a bit but I didn't know if the use of regex was a requirement. 我有些自由地简化了代码,但是我不知道是否需要使用正则表达式。

The filter method applies a filter against each data element, if the filter criteria , to coin a phrase, match, returning true retains the element. filter方法对每个数据元素应用一个过滤器,如果该过滤器的标准是匹配一个短语,则匹配,返回true将保留该元素。

The ternary operators are necessary to determine if an input is relevant to the match. 三元运算符是确定输入是否与匹配相关的必要条件。 if empty then data is not filtered on that criteria. 如果为空,则不会根据该条件过滤数据。

This last point is what I believe you were missing: 最后一点是我相信您所缺少的:

 const data = [ { label: { title: 'A title', }, character: 'R', type: 'first', }, { label: { title: 'Another title', }, character: 'R', type: 'second', }, { label: { title: 'A more interesting title', }, character: 'Z', type: 'second', }, ]; const criteria = { title: '', character: 'R', type: ['second'], }; const foundItems = data.filter(item=>{ let t = (criteria.title.length) ? item.label.title.includes(criteria.title) : true; let c = (criteria.character.length) ? item.character === criteria.character : true; let p = (criteria.type.length) ? criteria.type.includes(item.type) : true; return t && c && p; }); console.log(foundItems); 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM