javascript - ESLint error use Object.keys instead of for..in -
i'm working on below code works fine. but, eslint suggest use object.keys
instead of for..in
loop. tried iterate keys
, search if match found return object. works for..in
not object.keys. tried replace foreach
filter
didn't work me. suggestions.
function searchobj (obj, query) { // object.keys(obj).foreach(function(key){ (var key in obj) { var value = obj[key]; if (typeof value === 'object') { return searchobj(value, query); } if (typeof value === 'string' && value.tolowercase().indexof(query.tolowercase()) > -1) { return obj; } } } var demodata=[ {id:1,desc:{original:'trans1'},date:'2017-07-16'}, {id:2,desc:{original:'trans2'},date:'2017-07-12'}, {id:3,desc:{original:'trans3'},date:'2017-07-11'}, {id:4,desc:{original:'trans4'},date:'2017-07-15'} ]; var searchfilter = demodata.filter(function(obj){ return searchobj(obj, 'trans1'); }); console.log(searchfilter);
here link js bin
since don't use found object fact there. use array.prototype.some
avoid looping on every key in obj
have found matches search query.
function searchobj (obj, query) { return object.keys(obj).some(function(key) { var value = obj[key]; if (typeof value === 'object') { return searchobj(value, query); } return typeof value === 'string' && value.tolowercase().indexof(query.tolowercase()) > -1; }); } var demodata=[ {id:1,desc:{original:'trans1'},date:'2017-07-16'}, {id:2,desc:{original:'trans2'},date:'2017-07-12'}, {id:3,desc:{original:'trans3'},date:'2017-07-11'}, {id:4,desc:{original:'trans4'},date:'2017-07-15'} ]; var searchfilter = demodata.filter(function(obj){ return searchobj(obj, 'trans1'); }); console.log(searchfilter);
wiki
Comments
Post a Comment