我希望根据用户的输入更改过滤对象数组,并使用LODASH(https://lodash.com/)创建一个匹配的对象数组的新数组。
let listing = [{title: "Courier/Logistics", id: 1, totalWorkers: 1058, openPositions: 0, omani: 99.72},
{title: "Information Technology", id: 1, totalWorkers: 2, openPositions: 1, omani: 50},
{title: "Oil & Gas", id: 1, totalWorkers: 98, openPositions: 6, omani: 91.84}];
我尝试了lodash _.filter
,但这会过滤精确的单词和确切的大小写,然后返回单个搜索的值。
const filtered = _.filter(listing, function(o) {
return o.title === "oi";
});
console.log(filtered);
我希望像在mysql中一样得到结果:
WHERE title like %oi%
并给出包含该输入的匹配值。
由于
答案 0 :(得分:1)
看到片段, 用于lowerCase匹配大小写中的所有字符串, 在你的例子中,你的字符串“Oil& Gas”是Camel情况,当你尝试匹配字符串时,它总是会产生空白数组。
let listing = [{title: "Courier/Logistics", id: 1, totalWorkers: 1058, openPositions: 0, omani: 99.72},
{title: "Information Technology", id: 1, totalWorkers: 2, openPositions: 1, omani: 50},
{title: "Oil & Gas", id: 1, totalWorkers: 98, openPositions: 6, omani: 91.84}];
const filtered = _.filter(listing, function(o) {
return o.title.toLowerCase().indexOf("oi") > -1;
});
console.log(filtered);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>
答案 1 :(得分:0)
在过滤器功能中,使用包含功能 即
^=
答案 2 :(得分:0)
根本不需要lodash
,
只需使用正则表达式和本机过滤器:
listing.filter(o=>o.title.match(/oi/i))