javascript - jQuery iterate through loop delete elements -
i have javascript array of objects: each object contains key/value pairs. i'm trying iterate through array, , delete object value particular key (say "industry") fails match given value. here code, reason it's not looping through whole array, , think has fact when delete item loop counter botched somehow:
var industry = 'testing'; var = 0; (i = 0; < assets_results.length; i++) { var asset = assets_results[i]; var asset_industry = asset['industry']; if (industry != asset_industry) { assets_results.splice(i,1); } }
any ideas? in advance.
this because when splice 1 element, size of array decreases one. elements after splice shift 1 position beginning of array , fills space of spliced one. code misses 1 element.try code.
var industry = 'testing'; var = 0; (i = 0; < assets_results.length; i++) { var asset = assets_results[i]; var asset_industry = asset['industry']; if (industry != asset_industry) { assets_results.splice(i,1); i--; } }
Comments
Post a Comment