[英]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);
不需要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函數的摘要:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.