0

I have an array which contains multiple objects as below. How can i remove the complete object which contains particular value.

var cars=  [
{key: 'browser', label: 'Chrome'},
{key: 'browser', label: 'Firefox'},
{key: 'browser', label: 'Safari'}
];

For example the objects to be removed are those which contains label chrome and safari.

I have come across examples of single dimensional arrays of values, but this is an array of objects.

user3916007
  • 189
  • 1
  • 9

5 Answers5

2

You can use Array.prototype.filter to only return certain values or only exclude certain values.

var cars =  [
  {key: 'browser', label: 'Chrome'},
  {key: 'browser', label: 'Firefox'},
  {key: 'browser', label: 'Safari'}
];

// Filter by only the thing you want
var firefoxOnly = cars.filter(function(item) {
  return item.label === 'Firefox';
});

console.log(firefoxOnly);

// filter out the things you don't want
var notChromeSafari  = cars.filter(function(item) {
  return item.label !== 'Chrome' && item.label !== 'Safari';
});

console.log(notChromeSafari);

// or make a set of exclusions to filter out things you don't want
// you could also make this a list of inclusions, 
// it would be the inverse of this. I'll leave that as an exercise for the reader.
var exclusions = ['Chrome', 'Safari'];

var filtered  = cars.filter(function(item) {
  // only return items that are not in the exclusion list
  return exclusions.indexOf(item.label) === -1;
});

console.log(filtered);
JVDL
  • 1,157
  • 9
  • 16
0

Just loop through the array and remove the items that you don't want by checking if the property is the given value. Notice that you need to loop from last to first so that the index is correct on each iteration.

var cars=  [
    {key: 'browser', label: 'Chrome'},
    {key: 'browser', label: 'Firefox'},
    {key: 'browser', label: 'Safari'}
];

for(var i = cars.length -1; i >= 0 ; i--){
    if(cars.label == "Safari" || cars.label == "Chrome"){
        cars.splice(i, 1);
    }
}
Fraser
  • 15,275
  • 8
  • 53
  • 104
0

You could use Array filter function to achieve this.

var cars = [{
  key: 'browser',
  label: 'Chrome'
}, {
  key: 'browser',
  label: 'Firefox'
}, {
  key: 'browser',
  label: 'Safari'
}];

let removeKeys = ["Safari", "Chrome"];

let filteredArray = cars.filter((obj) => removeKeys.indexOf(obj.label) === -1);

console.log(filteredArray);
Sreekanth
  • 3,110
  • 10
  • 22
0

First you need to find the index of which object's label you want to delete.

var cars= [ {key: 'browser', label: 'Chrome'}, {key: 'browser', label: 'Firefox'}, {key: 'browser', label: 'Safari'} ];

for(var i = 0; i < cars.length ; i++)
{ 
     if(cars.label == "Safari" || cars.label == "Chrome")
     { 
           delete cars[i].label
     }
}

This will only remove the lebel. if you want to remove the entire element then use cars.splice(i, 1)

MD Ruhul Amin
  • 4,386
  • 1
  • 22
  • 37
  • This will delete the `label` property, but he wants to remove the object in which it occurs from the array. –  Nov 13 '16 at 05:20
0

How about this solution:

var cars=  [
 {key: 'browser', label: 'Chrome'},
 {key: 'browser', label: 'Firefox'},
 {key: 'browser', label: 'Safari'}
 ];

var arr = [];
for(var i = 0; i < cars.length; i++){
 if(cars[i].label !== 'Safari' && cars[i].label !== 'Chrome'){
  arr.push(cars[i]);
 }
}
console.log(arr);
HenryDev
  • 4,685
  • 5
  • 27
  • 64
  • This is going to return the single object with the non-matching label, but he wants to return an array of **all** the objects with non-matching labels. –  Nov 13 '16 at 05:19
  • @torazaburo Nope you are wrong. Look at the comment he made. He said he wants: {key: 'browser', label: 'Firefox'} and that's what I did :) – HenryDev Nov 13 '16 at 05:21
  • I don't see any where he says he wants that. He says *How can i remove the complete object which contains particular value*, which clearly indicates he wants an array with those objects removed. –  Nov 13 '16 at 05:27
  • @torazaburo Yup, he said he only wants the object and NOT an array. Do Control + F and search for HenryDev and you will see the comment he did to me. Again he only wants the object {key: 'browser', label: 'Firefox'} – HenryDev Nov 13 '16 at 05:29
  • You misread his comment. *Yes i just want only `{key: 'browser', label: 'Firefox'}`* He means he wants only that **in the resulting array** (because that's the only one that doesn't match Chrome or Safari; if there were additional elements in the input, presumably he would also want to preserve those in the output array. –  Nov 13 '16 at 05:33
  • @torazaburo I just updated my answer and the object is inside an array now. – HenryDev Nov 13 '16 at 20:38