简体   繁体   中英

Underscore.js filtering JSON

I have a JSON array like this.

[{
 SiteID: 350,
 CompanyList: [{
   CompanyName: "TestCompany",
   CompanyID: 30 }],
SiteName: "Testing"
}]

I am trying to _.filter like this.

var test = _.filter(jsondata, function(obj) {
  return ~obj.CompanyList.CompanyName.toLowerCase().indexOf('test');
});
console.log(test);

But i get undefined error. Any suggestions?

Because you want to filter on the CompanyList array which is the first element of the jsondata . You also need to complete the test for indexOf .

var test = _.filter(jsondata[0].CompanyList, function (obj) {
  return ~obj.CompanyName.toLowerCase().indexOf('test') > -1;
});

DEMO

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