20

I need to remove one element from an array and return only the remaining elements.

I tried with splice and filter but can't get it to work.

With splice it only returns the removed element, I need the opposite.

var parse_obj = JSON.parse(document.getElementById('imagens').value);
function rImagem(data){
    data = data - 1;
    document.getElementsByClassName("btn_remover")[data].style.display = 'none';
    parse_obj2 = parse_obj.splice(parse_obj.findIndex(e => e.data,1));
    new_string = JSON.stringify(parse_obj2);
    document.getElementById('imagens').value = new_string;
}
3
  • 2
    Why not just use parse_obj? .splice() changes the array in-place, so once you run parse_obj.splice it would be the array with the item removed. Commented Jan 24, 2019 at 11:18
  • splice returns the removed elements array. can you add random data ? filter should work Commented Jan 24, 2019 at 11:20
  • splice() method returns the deleted element from the array. You are trying to assign this deleted element in the array parse_obj2. What you should do is not assign anything to the variable parse_obj2 and use the parse_obj array as it is. parse_obj.splice(parse_obj.findIndex(e => e.data,1)); new_string = JSON.stringify(parse_obj2); Commented Jan 24, 2019 at 11:36

7 Answers 7

14

In your scenario you can use filter to filter the indexes you don't want in the resulting array. The first param of the callback you pass in the filter is the current element ele and the second is the index of the current element idx:

parse_obj2 = parse_obj.filter((ele, idx) => idx !== parse_obj.findIndex(e => e.data,1));

Here, lets say I want to remove the element at the third index so I would compare the current index in the filter function callback with the index of the element I want to remove. It will result in a new array with all the elements of the original array only without the element whose index I wanted to remove.

var indexToRemove = 2; 
var arr = [1, 2, 3, 4, 5];
var result = arr.filter((data, idx) => idx !== indexToRemove );
console.log(result);

The same result cam be obtained through splice also.

parse_obj.splice(parse_obj.findIndex(e => e.data,1), 1); //parse_obj is one element less.

Here is the demo:

var indexToRemove = 2; 
var arr = [1, 2, 3, 4, 5];
arr.splice(indexToRemove, 1); //removes only the third element and modifies the original array in place.
console.log(arr);

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

Comments

6

Array#splice is actually what you are looking for. The problem is, splice doesn't return the new array, it removes the element(s) from the array and returns the deleted elements. However, in the original array you will have the same elements except the removed ones.

let array = [1, 2, 3, 4, 5]

console.log(`Array elements before splice: ${array}`);
console.log(`Deleted elements: ${array.splice(1, 2)}`);
console.log(`Array elements after splice: ${array}`);

If you don't wish to modify the original array, though, you can always use filter:

let array = [1, 2, 3, 4, 5];
let toDelete = [2, 4];

console.log(`Original array elements: ${array}`);
console.log(`Elements to delete: ${toDelete}`);

let newArray = array.filter(n => !toDelete.includes(n));
console.log(`Array elements after splice: ${newArray}`);

Comments

3
    function remove(arr,index){
    return arr.slice(0,index).concat(arr.slice(index+1,9))
    }
    
   // remove(<your array over here>,<the index of the element that you want to remove>) 
   // ex -   
    remove(myarray,0);

Hope this helps

Comments

1

Hey I ran across a simple solution to this without using splice() using destructuring.

const myArr = [1,2,3,4,5,6,7,8,9,10];

function removeFirst(array){
const [a, ...arr] = array; //a is never returned
return arr;
}

Comments

0
  var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
    var filtered = array.filter(function(value, index, arr){ 
        return value > 5;
    });
    //filtered => [6, 7, 8, 9]
    //array => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
0

All you need is to know how to use splice. Hope this below answers your question.

const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// Inserts at index 1
console.log(months);
// Expected output: Array ["Jan", "Feb", "March", "April", "June"]

months.splice(4, 1,"May");
// Replaces 1 element at index 4
console.log(months);
// Expected output: Array ["Jan", "Feb", "March", "April", "May"]


months.splice(4, 1);
// Replaces 1 element at index 4
console.log(months);
// Expected output: Array ["Jan", "Feb", "March", "April" ]

Comments

0

arr.splice(0 , arr.length - 1 , 1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.