假设我有一个像这样的对象数组:
[{
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中实现这一目标的最佳方法是什么?谢谢!
答案 0 :(得分:4)
我的解决方案_.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);
答案 1 :(得分: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;
})
})
);
答案 2 :(得分:0)
您也可以使用普通的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;
)
}
)}
答案 3 :(得分:0)
我能想到的最短的是
// 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])))
答案 4 :(得分: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函数的摘要: