如何使用LoDash按关键字过滤对象数组?

5

假设我有一个像这样的对象数组:

[{
  away: "Seattle Seahawks",
  home: "Kansas City Chiefs",
},
{
  away: "Houston Texans",
  home: "San Francisco 49ers",
},
{
  away: "Dallas Cowboys",
  home: "Los Angeles Rams",
}]

需求:

搜索每个对象的关键词为49er,并返回第二个对象。

搜索每个对象的关键词为cow,并返回第三个对象。

搜索每个对象的关键词为an,并返回所有三个对象。

在lodash中实现这一需求的最佳方式是什么?谢谢!

5个回答

6

我使用_.flow实现的解决方案:

const my_arr = [{
  away: "Seattle Seahawks",
  home: "Kansas City Chiefs",
},
{
  away: "Houston Texans",
  home: "San Francisco 49ers",
},
{
  away: "Dallas Cowboys",
  home: "Los Angeles Rams",
}]

function flowFilter(array, substr) {
    return _.filter(array, _.flow(
    _.identity,
    _.values,
    _.join,
    _.toLower,
    _.partialRight(_.includes, substr)
  ));
}

const one = flowFilter(my_arr, '49er');
const two = flowFilter(my_arr, 'cow');
const three = flowFilter(my_arr, 'an');

console.log('one', one);
console.log('two', two);
console.log('three', three);

https://jsfiddle.net/1321mzjw/


2
不需要使用lodash库,可以使用原生JavaScript方法。

var data = [{
  away: "Seattle Seahawks",
  home: "Kansas City Chiefs",
}, {
  away: "Houston Texans",
  home: "San Francisco 49ers",
}, {
  away: "Dallas Cowboys",
  home: "Los Angeles Rams",
}];

console.log(
  // filter the array
  data.filter(function(v) {
    // get all keys of the object
    return Object.keys(v)
    // iterate and check for any object property value cotains the string
    .some(function(k) {
      // convert to lowercase(to make it case insensitive) and check match
      return v[k].toLowerCase().indexOf('49ers') > -1;
    })
  })
);

console.log(
  data.filter(function(v) {
    return Object.keys(v).some(function(k) {
      return v[k].toLowerCase().indexOf('an') > -1;
    })
  })
);


1
我能想到的最短的是:
// lodash
data.filter(d => _.some(d, t => /cow/i.test(t)))

// plain js
data.filter(d => Object.keys(d).some(k => /cow/i.test(d[k])))

1

您也可以使用纯JavaScript。

const my_arr = [{
  away: "Seattle Seahawks",
  home: "Kansas City Chiefs",
},
{
  away: "Houston Texans",
  home: "San Francisco 49ers",
},
{
  away: "Dallas Cowboys",
  home: "Los Angeles Rams",
}]



const filterByText = (arr, text) => {
  return arr.filter((item) => (
    !(
      item.away.toLowerCase().indexOf(text) === -1 && 
      item.home.toLowerCase().indexOf(text) === -1
    )
  )
)}

console.log(filterByText(my_arr, '49er')) // #2
console.log(filterByText(my_arr, 'cow')) // #3
console.log(filterByText(my_arr, 'an')) // #1, #2, #3

在这种情况下使用lodash几乎是相似的,因为你需要使用toLowerCase。
const filterByText = (arr, text) => {

  return _.filter(arr, (obj) => { 
    return Object.keys(obj).some((key) =>
      return obj[key].toLowerCase().indexOf(text) !== -1;
    )
  }

)}

0

我喜欢将正则表达式与lodash函数结合使用。这种情况是适用的。创建一个通用函数,接受一个集合和一个RegExp进行搜索:

function myFilter(coll, regex) {
  return _.filter(
    coll,
    _.unary(_.partialRight(_.some, _.method('match', regex)))
  );
}

现在你可以将任何正则表达式传递给myFilter(),它将针对coll中每个对象的所有属性值进行测试。以下是使用方法:

myFilter(data, /49er/);
// ➜ [
//     {
//       away: "Houston Texans",
//       home: "San Francisco 49ers",
//     }
//   ]

myFilter(data, /cow/i);
// ➜ [
//     {
//       away: "Dallas Cowboys",
//       home: "Los Angeles Rams",
//     }
//   ]

myFilter(data, /an/);
// ➜ [
//     {
//       away: "Seattle Seahawks",
//       home: "Kansas City Chiefs",
//     },
//     {
//       away: "Houston Texans",
//       home: "San Francisco 49ers",
//     },
//     {
//       away: "Dallas Cowboys",
//       home: "Los Angeles Rams",
//     }
//   ]

下面是使用 lodash 函数的摘要:

  • filter():通过在每个项目上评估谓词函数来过滤集合。
  • unary():使函数仅接受第一个参数,忽略所有其他参数。
  • partialRight():从右侧部分应用函数参数。当结果函数使用更多参数调用时,它们会向左移动。
  • some():如果集合中的任何项对于给定的谓词求值为 true,则返回 true。
  • method():在给定的对象参数上调用方法名。还接受参数以部分应用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接