How to delete a certain Object from an Array

83
August 22, 2018, at 11:20 AM

I have an Array as shown below

var test = 
[
  {
    "name": "Mike",
    "incentives": "23.45",
    "id": "1"
  },
  {
    "name": "Larsen",
    "incentives": "34.78",
    "id": "2"
  },
  {
    "name": "Steve",
    "incentives": "26.78",
    "id": "3"
  }
]

I want to remove a certain Object

I have tried as shown below

var idtoberemoved = 2
test = test.filter((obj) => typeof obj.id = '2');

https://jsfiddle.net/o2gxgz9r/65584/

Answer 1

You could find the index of the element in the array, and use splice to remove it.

var test = [{ 
    "name": "Mike", 
    "incentives": "23.45", 
    "id": "1" 
  }, 
  { 
    "name": "Larsen", 
    "incentives": "34.78", 
    "id": "2" 
  }, 
  { 
    "name": "Steve", 
    "incentives": "26.78", 
    "id": "3" 
  } 
]; 
 
//Use splice to remove the element 
test.splice( 
  //find the index of the element to be removed 
  test.indexOf(test.find(function(element){ return element.id === "2"; })) 
  //remove 1 element from the index found 
  , 1 
); 
 
console.log(test);

Answer 2

typeof obj.id will give you "string", and you want to filter out the objects whose id property is not equalt to "2". So, it should be

test.filter(obj => obj.id !== '2');

In JS, = is for assignment, not for equality test. We use ==, ===, !==, != for equality tests. There are difference between == and ===, but that is a whole subject.

Answer 3

    var test = [ 
        { 
            "name": "Mike", 
            "incentives": "23.45", 
            "id": "1" 
        }, 
        { 
            "name": "Larsen", 
            "incentives": "34.78", 
            "id": "2" 
        }, 
        { 
            "name": "Steve", 
            "incentives": "26.78", 
            "id": "3" 
        } 
    ]; 
     
    var filtered = test.filter(function(object) { 
     
        return object.id != 2; 
    }); 
     
    console.log(filtered);

Answer 4

If you are looking for more than one id being removed, here is one more way to accomplish using reduce. This is not very different from the other approaches and answers, but a different way to accomplish the ask.

var test = [{ 
    "name": "Mike", 
    "incentives": "23.45", 
    "id": "1" 
  }, 
  { 
    "name": "Larsen", 
    "incentives": "34.78", 
    "id": "2" 
  }, 
  { 
    "name": "Steve", 
    "incentives": "26.78", 
    "id": "3" 
  } 
]; 
 
function removeItem(idList) { 
 
  var result = test.reduce(function(output, currentObject) { 
    if (!idList.includes(currentObject.id)) { 
      output.push(currentObject); 
    } 
    return output; 
  }, []); 
  return result; 
} 
 
 
console.log(removeItem(["2", '3']))

READ ALSO
Prevent user from accessing form

Prevent user from accessing form

I have a button in my page which redirects the user to a form:

66
Clone jQuery selection into variable and insert it back into DOM

Clone jQuery selection into variable and insert it back into DOM

What am i doing wrong, isn't it possible to clone elements into a variable and insert it back into DOM from there?

96
Check if file is selected in Jquery

Check if file is selected in Jquery

I created a button with file inputIf I click on the button, I can choose a file from my computer

172
How i can get Cookie array value in all over the pages using jquery Cookie Plugin [on hold]

How i can get Cookie array value in all over the pages using jquery Cookie Plugin [on hold]

I have used jquery cookie plugin and get a function from the question Link Jquery Plugin Function and i have done to store an array of values and delete, add, clear all cookie array value

90