JavaScript deletes the elements from the array

javascript remove object from array
javascript array splice
remove element from array java
remove first element from array javascript
remove object from json array javascript
remove element from array python
javascript array filter
remove last element from array javascript

I have an array of integers, and I'm using the .push() method to add elements to it.

Why do I not delete the specified elements from the array when I use splice ()?

var arr = [1001, 1002, 1005, 1006];
var list = [{"id": 1002},{"id": 1005},{"id": 1006},{"id": 1007},{"id": 1008},{"id": 1009},{"id": 1010}];

function inArray(value, arr){
  for (let item in arr) {
    if (list.hasOwnProperty(item)) {
      if (arr[item] === value) {
        return true;
      }    
    }
  }
  return false;
};

for (let item in list) {
  if (list.hasOwnProperty(item)) {
    if (inArray(list[item].id, arr)) {
      list.splice(item, 1);
    }    
  }
};

console.log(list);

Try this:-

var arr = [1001, 1002, 1005, 1006];
var list = [{"id": 1002},{"id": 1005},{"id": 1006},{"id": 1007},{"id": 1008},{"id": 1009},{"id": 1010}];

function inArray(value){
  for (var i=0; i< arr.length; i++) {
      if (arr[i] === value) {
        return true;
      }    
  }
  return false;
};
var updatedList = [];
for (var i =0; i< list.length; i++) {
    if (!inArray(list[i].id)) {
    	updatedList.push(list[i])
    }    
};
console.log(updatedList);

JavaScript Array Methods, The array methods shift() and unshift() work on the beginning of an array instead of the end of an array, as is the case with push() and pop(). The  Step 2 - Remove elements from the end of a JavaScript array with pop() Step 3 - Remove elements from the start of a JavaScript array with shift() Step 4 - Use the 'Delete' function to remove individual array objects; Step 5 - Find and remove an element of a specific value; Step 6 - Find and remove multiple elements with the same value

One method is that

function deleteFromArray(list, arr) {
  let copy = []
  list.forEach((item, index) => {
    if (!arr.includes(item.id)) {
        copy.push(item)
    }
  })
  return copy
}

the other is

list.filter(item => {return !arr.includes(item.id)})

How can I remove a specific item from an array?, An integer indicating the number of elements in the array to remove from start . If deleteCount is omitted, or if its value is equal to or larger than  For deletion of elements in an array, two approaches can be used. They have their own merits regarding the way they perform the deletion. Using delete array[index]: This method deletes the element at the index specified, but does not modify the array. This means that at the place of the deleted index, the element is left undefined or null.

I will not give a code but will explain the reason why you are not getting the expected result. You are changing the list inside the loop iterating over the list. This has problems:

In the first iteration, 1002 is found in arr and your code removes the first entry from list. this is fine.

Second iteration: because you removed the first element of list, the remaining elements' indices decrease by 1. But this time item is 1. And even if you want to remove 1005 from arr, it's new index is 0. So instead 1006 gets deleted.

Third iteation: item is 2 and because you deleted 1002 and 1006, the 2nd element (starting from 0) in list is now 1008, which is not in arr; and nothing is deleted.

As the list changes in size, the total number of iterations will be 5 and NOT 7 because you have deleted two items while looping over list.

solution I suggest: iterate backward.

JavaScript: Remove Element from an Array, The delete operator only erases the value of the element and makes the element as undefined. The length of the array stays unaffected. If there are no elements, or the array length is 0, the method returns undefined. Using Splice to Remove Array Elements in JavaScript. The splice method can be used to add or remove elements from an array. The first argument specifies the location at which to begin adding or removing elements.

The problem with your code is when you do a .splice() on an array, indexes of the array will also change. That is why you are removing the wrong array element on the next loop.

You can use filter instead.

var arr = [1001, 1002, 1005, 1006];
var list = [{"id": 1002},{"id": 1005},{"id": 1006},{"id": 1007},{"id": 1008},{"id": 1009},{"id": 1010}];

list = list.filter(o => !arr.includes(o.id));

console.log(list);

Array.prototype.splice(), Using this index value we will then want to actually remove the element, which we can do with the splice() method. function removeElement(array, elem) { var  Summary: this tutorial shows you how to use the JavaScript Array’s splice() method to delete existing elements, insert new elements, and replace elements in an array. JavaScript Array type provides a very powerful splice() method that allows you to insert new elements into the middle of an array. However, you can use this method to delete and

Different ways to remove elements from an array in JavaScript, Remove Array elements by using pop() method: This method is used to remove the last element of the array and returns the removed element. This function  Results for array with 1.000.000 elements. In Chrome the array.splice (C) is the fastest in-place solution (the delete (C) is similar fast - but it left an empty slot in the array (so it does not perform a 'full remove')). The array.slice-splice (H) is the fastest immutable solution.

Remove Element from an Array in JavaScript, They have their own merits regarding the way they perform the deletion. Using delete array[index]:. This method deletes the element at the index specified, but  What is the easiest way to remove all elements from array that match specific string? from array that match specific string. array elements in JavaScript

Remove elements from a JavaScript Array, JavaScript offers many ways to remove an item from an array. Learn the canonical way, and also find out all the options you have, using plain  The first parameter (2) defines the position where new elements should be added (spliced in). The second parameter (0) defines how many elements should be removed. The rest of the parameters ("Lemon" , "Kiwi") define the new elements to be added. The splice() method returns an array with the deleted items:

Comments
  • Possible duplicate of For-each over an array in JavaScript?
  • You can use filter
  • Run the above code snippet. you will get your desired result in updatedList.