How to filter JSON Data in JavaScript or jQuery?
Solution 1:
This is how you should do it : ( for google find)
$([
{"name":"Lenovo Thinkpad 41A4298","website":"google222"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"}
])
.filter(function (i,n){
return n.website==='google';
});
Better solution : ( Salman's)
$.grep( [{"name":"Lenovo Thinkpad 41A4298","website":"google"},{"name":"Lenovo Thinkpad 41A2222","website":"google"}], function( n, i ) {
return n.website==='google';
});
http://jsbin.com/yakubixi/4/edit
Solution 2:
No need for jQuery unless you target old browsers and don't want to use shims.
var yahooOnly = JSON.parse(jsondata).filter(function (entry) {
return entry.website === 'yahoo';
});
In ES2015:
const yahooOnly = JSON.parse(jsondata).filter(({website}) => website === 'yahoo');