0

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/

2
  • 1
    it should be test.filter((obj) => obj.id === '2'); Commented Aug 21, 2018 at 18:36
  • once i execute this line , the array length is becoming 1 , where as i removed only one element so it must be 2 Commented Aug 21, 2018 at 18:45

4 Answers 4

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);

Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

The length is 1 , but it must be 2 know ,because i removed only 1 object from the array
@Pawan I am not getting you.
make it test.filter(obj => obj.id !== '2');
0

    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);

Comments

0

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']))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.