Tuesday, 15 May 2012

Delete or Filter object based on Boolean values using raw JavaScript or underscore.js -



Delete or Filter object based on Boolean values using raw JavaScript or underscore.js -

i have javascript object contains 2 objects different true/false boolean , wanted filter out first object has false values.

if in scenario, key true should retain, , shouldn't filter out info object.

i wanted maintain 2nd object based on true value, can see has 'true' values.

let me explain further, see first object of 'data' has boolean values 'false' should omitted , end result should have 2nd info object it's 'true' values.

"data": [{ "products": "mobile", "australia": false, "austria": false, "belgium": false, "canada": false, "china": false, "india": false, "poland": false, "brazil": false, "us": false }, { "products": "tablet", "australia": false, "austria": true, "belgium": false, "canada": false, "china": false, "india": true, "poland": false, "brazil": true, "us": false }]

end result should only, should filter first object because of false values.

"data": [{ "products": "tablet", "australia": false, "austria": true, "belgium": false, "canada": false, "china": false, "india": true, "poland": false, "brazil": true, "us": false }]

any suggestion using raw javascript or underscore.js welcome.

use filter , some.

data.filter( // filter info array function(product) { // looking @ each product in homecoming object.keys(product) // , product's keys .some( // see if of function(key) { // keys homecoming product[key] === true; // have value of true } ) ; } )

javascript underscore.js

No comments:

Post a Comment