最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Delete entire object if one property has certain value - Stack Overflow

programmeradmin0浏览0评论

I have an Array of objects that looks like this but a little bigger:

var total =   [{ cost="6.00",  descrip="tuna"},{ cost="5.50",  descrip="cod"}];

I need a way of deleting specific full objects from the array. Is it possible to identify the index of an object, based on the value of a property? The splice method looks like it could work if so.

total.splice(x,1);

Otherwise perhaps I could use the below in someway? can the objects in an array be given names and used with this somehow:

delete total[];

I have an Array of objects that looks like this but a little bigger:

var total =   [{ cost="6.00",  descrip="tuna"},{ cost="5.50",  descrip="cod"}];

I need a way of deleting specific full objects from the array. Is it possible to identify the index of an object, based on the value of a property? The splice method looks like it could work if so.

total.splice(x,1);

Otherwise perhaps I could use the below in someway? can the objects in an array be given names and used with this somehow:

delete total[];
Share Improve this question edited Aug 14, 2015 at 4:55 jacobsa 6,6382 gold badges33 silver badges69 bronze badges asked Jan 12, 2013 at 19:16 DVCITISDVCITIS 1,0373 gold badges18 silver badges37 bronze badges 1
  • You can have objects within objects, wich means you can set a name for an object or index it in some way, if that's what your after? – Richard Commented Jan 12, 2013 at 19:27
Add a ment  | 

5 Answers 5

Reset to default 5

Not really sure what your problem is. You first have to find which item you want to remove:

function findItem(arr) {
  for(var i = 0; i < arr.length; ++i) {
    var obj = arr[i];
    if(obj.cost == '5.50') {
      return i;
    }
  }
  return -1;
}

The findItem(total) function will return an index of an element matching cost == '5.50' condition (of course you can use another one). Now you know what to do:

var i = findItem(total);
total.splice(i, 1);

I'm assuming there is at least one object in the array matching the condition.

For ES5-pliant browsers, you can use filter(). E.g. to remove all items with cost < 6:

total = total.filter(function(item) {
  return item.cost < 6.0; 
});

Edit: Or even more concise version for ES6 environments:

total = total.filter(item => item.cost < 6.0);

This function deletes the first object in an array with object.keyName === value

function deleteIfMatches(array, keyName, value) {
    for (i=0; i<array.length; i++) {
        if (array[i][keyName] === value) {
           return array.splice(i, 1);
        }
    }
    // returns un-modified array
    return array;
}

I may have mis-understood, but isn't this quite simple, why do you want to splice?

var i = 0,
  count = total.length;

// delete all objects with descrip of tuna
for(i; i < count; i++) {
  if (total[i].descrip == 'tuna') { 
      delete total[i]
  }
}

Your objects won't be initialized unless you use colons instead of equals-

You can filter an array, the returned array does not contain any values but those that passed some test.

This returns an array of items that cost a dollar or more:

var total= [{
    cost:"6.00", descrip:"tuna"
},{
    cost:"5.50", descrip:"cod"
},{
    cost:".50", descrip:"bait"
}
].filter(function(itm){
return Number(itm.cost)>= 1;
});

/* returned value:

[{
        cost:'6.00',
        descrip:'tuna'
    },{
        cost:'5.50',
        descrip:'cod'
    }
]
发布评论

评论列表(0)

  1. 暂无评论