我有一个数组,我需要过滤它。如果我使用
,会发生什么var results = _.filter(arr, function (ob) {
return ob.VAL.indexOf(search) !== -1;
});
返回所有对象,因为它们包含'0 @ 1900-01-01'。我只想要返回第一个
search='0@1900'
arr=[
{
"DSR_MOTIVO": "Falta de nada",
"VAL": "0@1900-01-01@111@1900-01-01"
},
{
"DSR_MOTIVO": "Falta de Plano",
"VAL": "100@1900-01-01@111@1900-01-01"
},
{
"DSR_MOTIVO": "Fabrica Encerrada",
"VAL": "100@1900-01-01@150@1900-01-01"
},
{
"DSR_MOTIVO": "Refeicoes",
"VAL": "200@1900-01-01@212@1900-01-01"
},
{
"DSR_MOTIVO": "Lanche/WC",
"VAL": "200@1900-01-01@213@1900-01-01"
}
]
也许是正则表达式。
答案 0 :(得分:2)
如果你只是想要第一场比赛,那么_.find函数应该完成这项工作: https://lodash.com/docs/4.16.4#find
答案 1 :(得分:0)
解决_.startsWith
var results = _.filter(arr, function (ob) {
if(_.startsWith(ob.VAL, search)`enter code here`){
return ob.VAL;
}
});