过滤任何属性包含值的对象数组

Léo*_*oco 15 javascript arrays lodash

我想知道什么是最干净的方法,根据a过滤对象数组的更好方法string keyword.必须在对象的任何属性中进行搜索.

当我输入时,lea我想通过所有对象及其所有属性来返回包含的对象lea

当我输入时,italy我想通过所有对象及其所有属性来返回包含的对象italy.

我知道tere有很多解决方案,但到目前为止我只看到了一些你需要指定你想要匹配的属性.

ES6lodash欢迎!

  const arrayOfObject = [{
      name: 'Paul',
      country: 'Canada',
    }, {
      name: 'Lea',
      country: 'Italy',
    }, {
      name: 'John',
      country: 'Italy',
    }, ];

    filterByValue(arrayOfObject, 'lea')   // => [{name: 'Lea',country: 'Italy'}]
    filterByValue(arrayOfObject, 'ita')   // => [{name: 'Lea',country: 'Italy'}, {name: 'John',country: 'Italy'}]
Run Code Online (Sandbox Code Playgroud)

Nin*_*olz 35

您可以对其进行过滤并仅搜索一次搜索字符串.

使用的方法:

function filterByValue(array, string) {
    return array.filter(o =>
        Object.keys(o).some(k => o[k].toLowerCase().includes(string.toLowerCase())));
}

const arrayOfObject = [{ name: 'Paul', country: 'Canada', }, { name: 'Lea', country: 'Italy', }, { name: 'John', country: 'Italy' }];

console.log(filterByValue(arrayOfObject, 'lea')); // [{name: 'Lea', country: 'Italy'}]
console.log(filterByValue(arrayOfObject, 'ita')); // [{name: 'Lea', country: 'Italy'}, {name: 'John', country: 'Italy'}]
Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run Code Online (Sandbox Code Playgroud)

  • 如果没有`if`语句,你可以使用`return typeof o [k] ==='string'&& o [k] .toLowerCase().includes(string.toLowerCase()); (6认同)
  • 如果属性之一不是字符串而是对象,我们可能要使用此函数filterByValue(array,string){return array.filter(o => {return Object.keys(o).some(k => { if(typeof o [k] ==='string')返回o [k] .toLowerCase()。includes(string.toLowerCase());});}); }` (4认同)
  • 如果值不总是字符串,那么使用`String(o [k])`而不仅仅是'o [k]`.请参见此处:/sf/answers/775839081/ (2认同)

Vin*_*nie 8

使用 Object.keys 循环遍历对象的属性。使用 reduce 和 filter 使代码更高效:

 const results = arrayOfObject.filter((obj)=>{
     return Object.keys(obj).reduce((acc, curr)=>{
           return acc || obj[curr].toLowerCase().includes(term);
     }, false);
}); 
Run Code Online (Sandbox Code Playgroud)

其中 term 是您的搜索词。


bin*_*dMe 8

好吧,当我们已经知道它不会使用方法搜索对象时,可以执行以下操作来节省时间复杂度:

function filterByValue(array, value) {
  return array.filter((data) =>  JSON.stringify(data).toLowerCase().indexOf(value.toLowerCase()) !== -1);
}
Run Code Online (Sandbox Code Playgroud)

  • 是的,使用正则表达式可以避免这种情况。这里的想法是消除对象键上的循环 (3认同)
  • 这很有帮助。我也希望在这里找到一个例子。对于其他希望得到一个很好的例子的人来说,这里是我写的并且适用于我的案例。`JSON.stringify(terminal).replace(/("\w+":)/g, '').toLowerCase()`。这会将 `"{"myKey":"myValue","myKey2":"myValue2","mySubObject":{"subKey":"subValue"}}"` 转换为 `{"myValue","myValue2","subValue “}` (2认同)

Jos*_*eta 5

您始终可以使用array.filter()然后循环访问每个对象,如果任何值与您正在查找的值匹配,则返回该对象。

const arrayOfObject = [{
      name: 'Paul',
      country: 'Canada',
    }, {
      name: 'Lea',
      country: 'Italy',
    }, {
      name: 'John',
      country: 'Italy',
    }, ];
    
let lea = arrayOfObject.filter(function(obj){
  //loop through each object
  for(key in obj){
    //check if object value contains value you are looking for
    if(obj[key].includes('Lea')){
      //add this object to the filtered array
      return obj;
      }
     }
    });
      
console.log(lea);
Run Code Online (Sandbox Code Playgroud)