javascript - How to remove an object with a given property from an array -


this question has answer here:

i have array contains multiple objects below. how can remove complete object contains particular value.

var cars=  [ {key: 'browser', label: 'chrome'}, {key: 'browser', label: 'firefox'}, {key: 'browser', label: 'safari'} ]; 

for example objects removed contains label chrome , safari.

i have come across examples of single dimensional arrays of values, array of objects.

you can use array.prototype.filter return values or exclude values.

var cars =  [    {key: 'browser', label: 'chrome'},    {key: 'browser', label: 'firefox'},    {key: 'browser', label: 'safari'}  ];    // filter thing want  var firefoxonly = cars.filter(function(item) {    return item.label === 'firefox';  });    console.log(firefoxonly);    // filter out things don't want  var notchromesafari  = cars.filter(function(item) {    return item.label !== 'chrome' && item.label !== 'safari';  });    console.log(notchromesafari);    // or make set of exclusions filter out things don't want  // make list of inclusions,   // inverse of this. i'll leave exercise reader.  var exclusions = ['chrome', 'safari'];    var filtered  = cars.filter(function(item) {    // return items not in exclusion list    return exclusions.indexof(item.label) === -1;  });    console.log(filtered);


Comments

Popular posts from this blog

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

arrays - Algorithm to find ideal starting spot in a circle -

php - Autoloader issue not returning Class -