Remove object from array of objects

前端 未结 9 1444
滥情空心
滥情空心 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:34

    Do you need to keep the duplicate object?

    With underscoreJS

    var oldarray = [{"value":"14","label":"7"},{"value":"14","label":"7"},{"value":"18","label":"7"}]
    var newarray = _.reject(oldarray, function(o) { 
      return o == {"value":"14","label":"7"}; 
    });
    
    0 讨论(0)
  • 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.
    
    0 讨论(0)
  • 2021-01-31 15:45

    Try:

    var ar = [{"value":"14","label":"7"},{"value":"14","label":"7"},{"value":"18","label":"7"}];
    
    for(var i=0; i < ar.length; i++) {
       if(ar[i].value == "14" && ar[i].label == "7")
       {
          ar.splice(i,1);
       }
    }
    

    demo

    0 讨论(0)
  • 2021-01-31 15:45

    In ES6 (or using es6-shim) you can use Array.prototype.findIndex along with Array.prototype.splice:

    arr.splice(arr.findIndex(matchesEl), 1);
    
    function matchesEl(el) {
        return el.value === '14' && el.label === '7';
    }
    

    Or if a copy of the array is ok (and available since ES5), Array.prototype.filter's the way to go:

    var withoutEl = arr.filter(function (el) { return !matchesEl(el); });
    
    0 讨论(0)
  • 2021-01-31 15:46

    Using ES6 approach:

    const objIndex = objs.findIndex(o => o.value === '14' && o.label === '7')
    
    if(objIndex > -1) {
      objs.slice(objIndex, 1)
    }
    
    0 讨论(0)
  • 2021-01-31 15:55

    Here is a way to do it without 'creating a new array' or using external libraries. Note that this will only remove the first instance found, not duplicates, as stated in your question's example.

    // Array to search
    var arr = [{"value":"14","label":"7"},{"value":"14","label":"7"},{"value":"18","label":"7"}];
    
    // Object to remove from arr
    var remove = {"value":"14","label":"7"};
    
    // Function to determine if two objects are equal by comparing
    // their value and label properties
    var isEqual = function(o1, o2) {
        return o1 === o2 || 
            (o1 != null && o2 != null && 
             o1.value === o2.value &&
             o1.label === o2.label);
    };
    
    // Iterate through array and determine if remove is in arr
    var found = false;
    var i;
    for(i = 0; i < arr.length; ++i) {
        if(isEqual(arr[i], remove)) {
            found = true;
            break;
        }
    }
    
    if(found) {
        // Found remove in arr, remove it
        arr.splice(i, 1);
    }
    

    Demo

    0 讨论(0)
提交回复
热议问题