Remove object from array of objects

前端 未结 9 1450
滥情空心
滥情空心 2021-01-31 15:28

I have an array of objects:

[{\"value\":\"14\",\"label\":\"7\"},{\"value\":\"14\",\"label\":\"7\"},{\"value\":\"18\",\"label\":\"7\"}]

How I ca

9条回答
  •  梦如初夏
    2021-01-31 15:37

    I would create a new array...

    var original = [{"value":"14","label":"7"},{"value":"14","label":"7"},{"value":"18","label":"7"}]
    
    var result = [];
    
    for (var i = 0, l = original.length; i < l; i++) { // Traverse the whole array
      var current = original[i];
      if (! (current.value == 14 && current.label == 7) ) {
        // It doesn't match the criteria, so add it to result
        result.push( current );
      }
    }
    

    Edit: I've read your question once more. You only want to remove the first element? Then use slice to get only a part of the array.

    var result = original.slice(1, original.length)
    

    or splice

    original.splice(0,1); // Remove one element from index 0.
    

提交回复
热议问题