简体   繁体   中英

filter return true or false

I'm using filter to find id in data.it return object not true or false.

How can I return true or false if val.recoredId === valueId ?

var hasMatch = data.filter(function (val) {
  return (val.recordId === valueId);
});
        

Just check the length of filtered results.

var hasMatch = data.filter(function (val) {
  return (val.recordId === valueId);
}).length > 0;

使用查找

 hasMatch = data.find(function (value) {return value.recordId == valueId });

While calling filter with length appended does resolve this, I would point you to the some() method

hasMatch = data.some(function (value) {
   return value.recordId == valueId
});

This will return a boolean if the array contains any matching value.recordId == valueId entries

try this

var hasMatch = data.filter(function (val) {
    return !!(val.recordId === valueId);                               
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM