簡體   English   中英

如何使用LoDash按關鍵字過濾對象數組?

[英]How do I filter an array of objects by keyword using LoDash?

假設我有一個像這樣的對象數組:

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

要求:

搜索每個對象和關鍵字49er並讓它返回對象#2。

在每個對象中搜索關鍵字cow並讓它返回對象#3。

在每個對象中搜索關鍵字an並讓它返回所有三個對象。

在lodash中實現這一目標的最佳方法是什么? 謝謝!

我用_.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/

不需要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; }) }) ); 

您也可以使用普通的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幾乎是相似的,因為您需要使用LoverCase。

const filterByText = (arr, text) => {

  return _.filter(arr, (obj) => { 
    return Object.keys(obj).some((key) =>
      return obj[key].toLowerCase().indexOf(text) !== -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])))

我喜歡將正則表達式與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() :在給定的對象參數上調用方法名稱。 還接受部分應用的參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM