var arr = [1, 2, 3, 4, 5]
console.log(arr)
//[1, 2, 3, 4, 5]
arr = arr.splice(4, 1)
console.log(arr)
//[5]
I want to remove only the last element. Why does it remove every element except the last one?
You need to omit the assignment.
var arr = [1, 2, 3, 4, 5] console.log(...arr); // [1, 2, 3, 4, 5] arr.splice(4, 1); console.log(...arr); // [1, 2, 3, 4]
If you like just to delete the last one, you could take a negative index with Array#splice
. This works for any other index from the end.
var arr = [1, 2, 3, 4, 5] console.log(...arr); // [1, 2, 3, 4, 5] arr.splice(-1, 1); console.log(...arr); // [1, 2, 3, 4]
Array#splice
modifies the array in place and returns the removed portion. You should not reassign the removed portion to the variable.
var arr = [1, 2, 3, 4, 5] arr.splice(4, 1) console.log(arr);
splice
alters the array and returns the removed item(s) in a new array.
By the way, you should use pop
which is better suited for this like so:
var arr = [1, 2, 3, 4, 5]; arr.pop(); console.log(arr);
pop
removes the last item of the array.
The splice method takes two parameters: a beginning index (inclusive) and an ending index (exclusive). The below will do what you described.
arr = arr.splice(0, 4)
This is a more general approach:
var arr = [1, 2, 3, 4, 5] arr.splice(arr.length - 1); console.log(arr)
You can access to the last arr element by splice(-1), then you stored that value (5) into a variable (newArr). The original array now values [1, 2, 3, 4].
let arr = [1, 2, 3, 4, 5]; let newArr = arr.splice(-1); console.log(arr) //[1, 2, 3, 4] console.log(newArr) //[5]
In your code, you are reassigning the arr to the spliced value, which is [5]. the way you should do it is,
var arr = [1, 2, 3, 4, 5]
console.log(arr)
//[1, 2, 3, 4, 5]
arr.splice(4, 1)
console.log(arr)
//[1, 2, 3, 4]
This should give you the result you are looking for;
The splice() method adds/removes items to/from an array, and returns the removed items in the form of an array.
Syntax: let arrDeletedItems = array.splice(starting_index, index to which you want to add or remove an item, item 1, item 2, item 3...so on )
let arr = [1, 2, 3, 4, 5] arr.splice(4, 1)
so in your case, you are starting from index 4 which means the value "5" and you are removing the 1st element that starts from index 4 which is the value '5'. So the splice method would return a new array (as it returns a new array of the removed elements) containing only 5.
To get back an array containing all the elements except the last element 5 you should write: arr = arr.splice(0,4) which means that start from index 0 and remove elements till index 4.
You can also remove the last element from the array with rest operator
const array = [1, 2, 3, 4, 5]
const [ ...newArray, last ] = array
Splice method returns the removed item(s) in an array so what is happening is you are assigning the returned to the original array. Instead do this
var arr = [1, 2, 3, 4, 5];
console.log(arr);
//[1, 2, 3, 4, 5]
arr.splice(4, 1);
console.log(arr);
Splice modifies array in place and returns removed part as a new array. To get expected result you want to use expression:
var arr = [1, 2, 3, 4, 5]
console.log(arr)
//[1, 2, 3, 4, 5]
arr.splice(4, 1)
console.log(arr)
//[5]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.