我有以下数组。
var arr = [1,0,2];
我想要移除最后一个元素2。
我使用了arr.slice(-1);
,但它并没有移除该值。
我有以下数组。
var arr = [1,0,2];
我想要移除最后一个元素2。
我使用了arr.slice(-1);
,但它并没有移除该值。
值得注意的是,slice
方法会返回一个 新的 数组,而 .pop()
和 .splice()
方法则会在 现有 的数组上进行修改。
如果你喜欢使用链式命令风格处理数据集合,那么像这样的情况你就真的需要坚持使用 slice
方法。
例如:
myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var newArrayOfThings = myArray
.filter(x => x > 5) // only bigly things
.slice(0, -1) // get rid of the last item ! slice(-1) will give first element
.map(x => `The number is: ${x}`);// map to a set of strings
使用"pop"进行相同类型的操作需要更多的处理和变量管理,因为与map
、filter
等不同,它不会返回新数组。
push
也是一样的情况,它将项添加到数组的末尾。如果使用concat
,则可以保持流畅性,可能效果更好。
myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var newArrayOfThings = myArray
.filter(x => x > 5) // only bigly things
.slice(-1) // get rid of the "10"
.concat([100]) // note with concat, you must pass an array
.map(x => `The number is: ${x}`) // map to a set of strings
splice()
这两种方法之一完成:arr.splice(-1,1)
arr.splice(arr.length-1,1)
splice(position_to_start_deleting, how_many_data_to_delete)
接受两个参数。
position_to_start_deleting
:从哪里开始删除,从0开始索引。
how_many_data_to_delete
:从指定的索引开始,应该删除多少连续数据。
你也可以使用pop()
来移除最后一个元素,因为pop()
可以从某个数组中删除最后一个元素。请使用arr.pop()
。
var arr = [ 0, 1, 2, 3, 4, 5, 6, 7 ];
// using slice
arr = arr.slice(0, -1);
console.log('arr : ', arr);
// using splice
arr.splice(-1);
console.log('arr : ', arr);
// using pop
arr.pop();
console.log('arr : ', arr);
// using shift and reverse
arr.reverse().shift()
arr.reverse();
console.log('arr : ', arr);
// using spread Operator and reverse
const [, ...exceptLast] = arr.reverse();
arr = exceptLast.reverse();
console.log('arr : ', arr);
// removing last index
arr.length -= 1;
console.log('arr : ', arr);
length
的副作用是否会导致内存泄漏或类似问题?(您在Joby Joseph的评论中提出的最后一个建议arr.length -= 1;
)否则,这是一个简洁的解决方案。另一个注意点:如果您像第二个建议中那样使用.splice(-1,1)
,则可以省略,1
,因为splice(-x)
会删除从倒数第x
个开始的所有元素。对于-x
的-1
,从“最后一个”开始的所有元素与“最后一个”完全相同。 - Cadoizarr.length -= 1;
和 arr.length = arr.length - 1;
的含义相同。 - Sahil Thummar++arr.length
给出了这样的结果:“arr:”,[0, 1, undefined],这表明它是可以的。 - Cadoiz另一种方法是基于索引进行筛选:
arr.filter((element, index) => index < arr.length - 1);
filter()
会创建一个新的数组,而不是改变已有的数组。这种方法更方便地删除和存储数组的最后一个元素。
var sampleArray = [1,2,3,4];// Declaring the array
var lastElement = sampleArray.pop();//this command will remove the last element of `sampleArray` and stores in a variable called `lastElement` so that you can use it if required.
console.log(sampleArray); //This will give you [1,2,3]
console.log(lastElement); //this will give you 4
var a = [1,2,3,4,5,6];
console.log(a.reverse().slice(1).reverse());
//Array(5) [ 1, 2, 3, 4, 5 ]
splice(index,howmany) - 这个解决方案听起来不错。但是这个howmany只适用于正数组索引。要删除最后两项或三项,请使用索引本身。
例如,使用splice(-2)删除最后两个项目。使用splice(-3)删除最后三个项目。
_.dropRight([1, 2, 3])
// => [1, 2]
_.dropRight([1, 2, 3], 2);
// => [1]
var arr = [1,0,2];
arr.length--;
移除最后一个元素,但你可能需要先检查arr.length > 0
。
2019年ECMA5解决方案:
const new_arr = arr.reduce((d, i, idx, l) => idx < l.length - 1 ? [...d, i] : d, [])
无损、通用、一行代码即可,只需在您的数组末尾进行复制和粘贴。
arr.slice(0,-1)
对我来说是最佳解决方案。 - Black Mambasplice()
方法;对于第二种情况,可以使用pop()
方法。 - thoni56arr.splice(-1,1)
会返回数组[1,0]
;arr.slice(-1,1)
会返回[2]
" - Cadoiz