mm1975
mm1975

Reputation: 1655

Delete item from object

I have an object which looks like this:

{
 "myValues": [
   {
      "begin": 514,
      "end": 597,
      "type": "cars"
   },
   {
      "begin": 514,
      "end": 597,
      "type": "shoes"
   },
   ....

Now I would like to remove the type shoes and all data in its array.

I´m trying to do like this, but it do not work:

for (var item in obj) {

    var type = obj[item].type;

    if (type == 'shoes') {
        console.log('deleted');
        delete obj[item].type;
    }
}

Upvotes: 0

Views: 51

Answers (5)

Mamun
Mamun

Reputation: 68933

Based on the structure of your object, you have to use obj.myValues so that your code works as the way you expect:

var obj = {
"myValues": [
 {
    "begin": 514,
    "end": 597,
    "type": "cars"
 },
 {
    "begin": 514,
    "end": 597,
    "type": "shoes"
 }]}
for (var item in obj.myValues) {
  var type = obj.myValues[item].type;

  if (type == 'shoes') {
      console.log('deleted');
      delete obj.myValues[item].type;
  }
}

console.log(obj);

Upvotes: 2

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

You can use filter() together with destructuring to shorten the code:

var obj = [
   {
      "begin": 514,
      "end": 597,
      "type": "cars"
   },
   {
      "begin": 514,
      "end": 597,
      "type": "shoes"
   },
   {
      "begin": 514,
      "end": 597,
      "type": "shoes"
   },
   {
      "begin": 52,
      "end": 512,
      "type": "cars"
   }
];
var res =  obj.filter(({type}) => type!== "shoes");
console.log(res);

Upvotes: 0

Vignesh Raja
Vignesh Raja

Reputation: 8731

While Array.filter() can be used, here is an another approach using Array.reduce().

var data = {"myValues": [
   {
      "begin": 514,
      "end": 597,
      "type": "cars"
   },
   {
      "begin": 514,
      "end": 597,
      "type": "shoes"
   }]};
   
var result = data.myValues.reduce(function(acc, value, index){
    if(value.type=="shoes") acc.push(value);
    return acc;
},[])

console.log(result);

Upvotes: 0

brk
brk

Reputation: 50291

delete keyword works on removing key from an object, but here the requirement seems to remove complete object.If it is so then you can use filter method

var obj = {
  "myValues": [{
      "begin": 514,
      "end": 597,
      "type": "cars"
    },
    {
      "begin": 514,
      "end": 597,
      "type": "shoes"
    }
  ]
}

obj.myValues = obj.myValues.filter(function(item) {
  return item.type !== 'shoes'
})
console.log(obj)

Upvotes: 0

Fredrik Widerberg
Fredrik Widerberg

Reputation: 3108

You need to iterate one level deeper

var obj = {
 "myValues": [
   {
      "begin": 514,
      "end": 597,
      "type": "cars"
   },
   {
      "begin": 514,
      "end": 597,
      "type": "shoes"
   }
]};


for (var it in obj) {
    var value = obj[it];
    for(var it2 in value) {
        if (value[it2].type == 'shoes') {
            console.log('deleted');
            delete value[it2].type;
        }
    }
}

Upvotes: 0

Related Questions