typescript Javascript 如何在 filter() 中使用 forEach() 过滤数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44997344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript how to filter an array using forEach() inside filter()
提问by celsomtrindade
I have an array of objects and I'd like to filter it based on the objects property values. I'd like to filter it by different properties, so it needed to be dynamic. For this I have an input field where I type and then filter the array. So, let's say I have these 2 different arrays:
我有一个对象数组,我想根据对象属性值对其进行过滤。我想通过不同的属性过滤它,所以它需要是动态的。为此,我有一个输入字段,我在其中键入然后过滤数组。所以,假设我有这两个不同的数组:
const array_one = [
{id: 1, code: 'ABC123', name: 'John'},
{id: 2, code: 'DEF456', name: 'Stew'},
// ...
];
const array_two = [
{id: 1, value: '012345', company: 'Company 01' },
{id: 2, value: '678910', company: 'Company 02' },
// ...
];
I want a function where I can filter the first array based on the name
, also If I want to filter the second array, I want to filter it by the value
.
我想的函数,其中我可以筛选基于所述第一阵列name
,也如果我想第二阵列筛选,我要通过对其进行过滤value
。
For this, I built this function:
为此,我构建了这个函数:
filterArray(array: Array<any>, fields: Array<any>, value: string) {
value = this.convertString(value);
array = array.filter((item) => {
fields.forEach(obj => {
if ( item[obj] ) {
const _newObj = this.convertString(item[obj]);
if ( _newObj.indexOf(value) !== -1 ) {
console.log(item);
return item;
}
}
});
});
return array;
}
// convertString() is just another function to replace accents, spaces, etc...
Then I call it like this:
然后我这样称呼它:
filterArray(originalArray, ['name'], valueFromInput);
// or...
filterArray(originalArray, ['value'], valueFromInput);
// or even...
filterArray(originalArray, ['value', 'company'], valueFromInput);
But the array filtered is always returnin empty, even if the console inside the indexOf
verification prints the correct object on the console.
但是过滤后的数组总是返回空的,即使indexOf
验证里面的控制台在控制台上打印了正确的对象。
What am I doing wrong here? Because it's filtering properly, I have manually checked it, but it doesn't add to the new filtered array.
我在这里做错了什么?因为它过滤正确,我已经手动检查过它,但它没有添加到新的过滤数组中。
回答by Ori Drori
You can iterate the fields
using Array#some
, and if one of them is equal to value
return the item
:
您可以迭代fields
using Array#some
,如果其中之一等于value
返回item
:
const array_one = [
{id: 1, code: 'ABC123', name: 'John'},
{id: 2, code: 'DEF456', name: 'Stew'}
];
const array_two = [
{id: 1, value: '012345', company: 'Company 01' },
{id: 2, value: '678910', company: 'Company 02' }
];
const filterArray = (array, fields, value) => {
fields = Array.isArray(fields) ? fields : [fields];
return array.filter((item) => fields.some((field) => item[field] === value));
};
console.log(filterArray(array_one, 'name', 'Stew'));
console.log(filterArray(array_two, ['id', 'company'], 2));
console.log(filterArray(array_two, ['id', 'company'], 'Company 02'));
回答by Abdul Aziz Sabra
If you want to make the filter about more than one filed then the value that you send it to the function, should be also array.In my code below I assume that you want to return the object that Achieves all conditions (contains all properties that you send to the function with the same value)
如果您想让过滤器包含多个字段,那么您将其发送给函数的值也应该是数组。在我下面的代码中,我假设您要返回满足所有条件的对象(包含您发送给具有相同值的函数的所有属性)
const array_one = [
{id: 1, code: 'ABC123', name: 'John'},
{id: 2, code: 'DEF456', name: 'Stew'},
];
const array_two = [
{id: 1, value: '012345', company: 'Company 01' },
{id: 2, value: '678910', company: 'Company 02' },
];
function filterArray(array, fields, value) {
array = array.filter((item) => {
const found = fields.every((field, index) => {
return item[field] && item[field] === value[index]
})
return found
});
return array;
}
console.log(filterArray(array_one, ['name'], ['Stew']));
console.log(filterArray(array_two, ['id', 'company'], [1,'Company 01']));