0

Why does the new array only contain the value '394', instead of all the other ones?

const values = [5, 11, 394, 2, 576];  

function pureSplice(givenArray: number[]){  
    const newArray: number[] = givenArray.splice(2, 1).map(x => x);  
    return newArray;  
}

pureSplice(values);

The 'newArray' only contains the value '394', why does it do that and is there a way to get the array to have all values other than 394' with .splice and .map?

2
  • Check this. I think it solves your problem. Commented Jan 30, 2023 at 13:00
  • splice returns an array of the removed items, not a reference to the updated array. Also note that it modifies the array you call it on, which I suspect isn't your intention. See the answers to the linked questions for details and what to do instead. Commented Jan 30, 2023 at 13:00

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.