简体   繁体   English

筛选数组对象中选定键中的搜索

[英]filter search in array object, in selected keys

I need some suggestion or the best way to get it to array data filter with the passed value. 我需要一些建议或最佳方法,以使其具有传递的值到数组数据过滤器中。 Currently, I have 目前,我有

let arrayobject = [
{"name": "apple", "type": "juice-material", "srnum": 123234},
{"name": "cranberry", "type": "desert-material", "srnum": 98989},
{"name": "grapes", "type": "wine-material", "srnum":656565}]

if I passed apple then return array will be 如果我通过apple那么返回数组将是

[{"name": "apple", "type": "juice-material", "srnum": 123234}]

if I passed wine-material return result 如果我通过了wine-material退还结果

{"name": "grapes", "type": "wine-material", "srnum":656565}

similarly for srnum, while passing it gives filter array object result. 与srnum类似,在传递时会给出过滤器数组对象结果。

Currently, 目前,

I have tried using 我尝试使用

_.some(arrayobject, _.unary(_.partialRight(_.includes, passedValue)))

it gives context result, but not predictable. 它给出了上下文结果,但不可预测。

Let me know the best possible way to filter the data. 让我知道过滤数据的最佳方法。 Thanks. 谢谢。

Well, I think your approach is pretty much the best. 好吧,我认为您的方法几乎是最好的。 However, you use _.some which will merely give a boolean result if any item matches, if you swap it with _.filter you can get all items that match or you can change for _.find to just get the first one: 但是,您使用_.some只会在任何项目匹配时给出布尔结果,如果将其与_.filter交换,则可以获取所有匹配的项目,也可以更改_.find以获得第一个:

 let arrayobject = [ {"name": "apple", "type": "juice-material", "srnum": 123234}, {"name": "cranberry", "type": "desert-material", "srnum": 98989}, {"name": "grapes", "type": "wine-material", "srnum": 656565} ] function filterByValues(collection, whatToSearchFor) { //separating just for easier readability let predicate = _.unary(_.partialRight(_.includes, whatToSearchFor)); return _.filter(collection, predicate); } //matches a value console.log(filterByValues(arrayobject, "apple")); console.log(filterByValues(arrayobject, "wine-material")); console.log(filterByValues(arrayobject, 98989)); //doesn't match a key console.log(filterByValues(arrayobject, "name")); console.log(filterByValues(arrayobject, "type")); 
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script> 

With that said, @georg points out in a comment that you can re-write this using less Lodash functionality and just a plain callback with _.includes - this might be a bit more readable than using _.partialRight and _.unary : 话虽如此,@ georg在注释中指出您可以使用较少的Lodash功能并仅使用_.includes进行简单的回调来_.includes_.includes -与使用_.partialRight_.unary相比,此代码可能更具可读性:

 let arrayobject = [ {"name": "apple", "type": "juice-material", "srnum": 123234}, {"name": "cranberry", "type": "desert-material", "srnum": 98989}, {"name": "grapes", "type": "wine-material", "srnum": 656565} ] function filterByValues(collection, whatToSearchFor) { //separating just for easier readability let predicate = item => _.includes(item, whatToSearchFor); return _.filter(collection, predicate); } //matches a value console.log(filterByValues(arrayobject, "apple")); console.log(filterByValues(arrayobject, "wine-material")); console.log(filterByValues(arrayobject, 98989)); //doesn't match a key console.log(filterByValues(arrayobject, "name")); console.log(filterByValues(arrayobject, "type")); 
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script> 

You can use arrayobject.filter() to filter on desired prop 您可以使用arrayobject.filter()过滤所需的道具

 let arrayobject = [ {"name": "apple", "type": "juice-material", "srnum": 123234}, {"name": "cranberry", "type": "desert-material", "srnum": 98989}, {"name": "grapes", "type": "wine-material", "srnum":656565}] var res = arrayobject.filter(x => x.name == "apple"); console.log(res); var res2 = arrayobject.filter(x => x.type == "wine-material"); console.log(res2); 

The filter can be applied as follows: 该过滤器可以按以下方式应用:

 function filterItems(items, searchVal) { return items.filter((item) => Object.values(item).includes(searchVal)); } let arrayobject = [ { "name": "apple", "type": "juice-material", "srnum": 123234 }, { "name": "cranberry", "type": "desert-material", "srnum": 98989 }, { "name": "grapes", "type": "wine-material", "srnum": 656565 }] console.log("Filtered by name: ", filterItems(arrayobject, "apple")); console.log("Filtered by type: ", filterItems(arrayobject, "desert-material")); console.log("Filtered by srnum: ", filterItems(arrayobject, 656565)); 

You can use flatMap with the required array. 您可以将flatMap与所需的数组一起使用。 All you need to give properties which you want to find. 您只需提供要查找的属性。

In your case it is 你的情况是

  let arrayobject = [ {"name": "apple", "type": "juice-material", "srnum": 123234}, {"name": "cranberry", "type": "desert-material", "srnum": 98989}, {"name": "grapes", "type": "wine-material", "srnum":656565}] var filteredData = getFilterData(arrayobject, 656565); console.log(filteredData); function getFilterData(array, searchValue){ return array.flatMap(obj => obj) .find(({ name,type,srnum }) => (name === searchValue || type == searchValue) || srnum == searchValue); } 

You can use filter() on your array Object. 您可以在数组对象上使用filter() The filter() method creates a new array with all elements that pass the test implemented by the provided function. filter()方法创建一个新数组,其中所有元素都通过了由提供的函数实现的测试。

arrayobject.filter(data => {
return (data.name && data.name.includes(value)) ||
       (data.type && data.type.includes(value)) ||
       (data.srnum && data.srnum.includes(value))
})

If you want to filter objects from array, which contains the value in any on the key then you can directly use an _.includes in each object to filter ( _.filter ) them out from array. 如果要从数组中筛选出包含键中任何值的对象,则可以直接在每个对象中使用_.includes从数组中筛选出( _.filter )。 in case you need to look on a specific set of keys, just use _.pick before passing the object to includes. 如果需要查看一组特定的键,只需在将对象传递给_.pick之前使用_.pick

 let array = [ {id: 1, x: 'XX1', y: 'YY1', z: 'ZZ1'}, {id: 2, x: 'XX1', y: 'YY2', z: 'ZZ2'}, {id: 3, x: 'ZZ2', y: 'YY3', z: 'ZZ1'}, ], filterByValue = (a, v) => _.filter(a, e => _.includes(e, v)), filterByValueInKeys = (a, v, keys) => _.filter(a, e => _.includes(_.pick(e, keys), v)); console.log('ZZ1: ', filterByValue(array, 'ZZ1')); console.log('ZZ2: ', filterByValue(array, 'ZZ2')); console.log('ZZ1 within x,y: ', filterByValueInKeys(array, 'ZZ1', ['x', 'y'])); console.log('ZZ2 within x,y: ', filterByValueInKeys(array, 'ZZ2', ['x', 'y'])); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script> 

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

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