如何在JavaScript中从数组中删除元素? 移除第一个元素
我要删除数组的第一个元素,使其成为:如何在JavaScript中从数组中删除元素? 移除第一个元素,javascript,arrays,Javascript,Arrays,我要删除数组的第一个元素,使其成为: var arr = [1,2,3,5,6]; var arr = [2,3,5,6]; 移除第二个元素 为了扩展这个问题,如果要删除数组的第二个元素,使其成为: var arr = [1,2,3,5,6]; var arr = [2,3,5,6]; 要获得更灵活的解决方案,请使用函数。它允许您根据索引值删除数组中的任何项: var arr = [1,3,5,6]; 也许是这样的: var indexToRemove = 0; var numberT
var arr = [1,2,3,5,6];
var arr = [2,3,5,6];
移除第二个元素
为了扩展这个问题,如果要删除数组的第二个元素,使其成为:
var arr = [1,2,3,5,6];
var arr = [2,3,5,6];
要获得更灵活的解决方案,请使用函数。它允许您根据索引值删除数组中的任何项:
var arr = [1,3,5,6];
也许是这样的:
var indexToRemove = 0;
var numberToRemove = 1;
arr.splice(indexToRemove, numberToRemove);
非常适合你的情况shift()
从数组中删除第一个元素并返回该元素。此方法更改数组的长度
arr=arr.slice(1);
写了一篇关于在Javascript数组中任意位置插入和删除元素的小文章 下面是从任何位置删除元素的小片段。这扩展了Javascript中的数组类,并添加了remove(index)方法 因此,要删除示例中的第一项,请调用arr.remove(): 要删除第二项
var arr = [1,2,3,5,6];
arr.remove(0);
下面是一个关于Array类的insert和delete方法的小示例
本质上,这与使用splice的其他答案没有什么不同,但是名称splice
是不直观的,如果在应用程序中都有该调用,它只会使代码更难阅读
arr.remove(1);
是非破坏性的,splice and shift将修改原始数组数组。prototype.shift方法从数组中删除第一个元素,并返回它。它修改原始数组
arr.slice(begin[,end])
let arr = [1, 2, 3]
arr.reduce((xs, x, index) => {
if (index == 0) {
return xs
} else {
return xs.concat(x)
}
}, Array())
// Or if you like a oneliner
arr.reduce((xs, x, index) => index == 0 ? xs : xs.concat(x), Array())
function removeElementAtIndex<T>(input: T[], index: number) {
return input.slice(0, index).concat(input.slice(index + 1));
}
Array.splice()
具有一个有趣的属性,即不能使用它删除第一个元素。因此,我们需要求助于
var a = [1,2,3]
// [1,2,3]
a.shift()
// 1
a
//[2,3]
其他答案都很好,我只是想添加一个带有
ES6
数组函数的替代解决方案:过滤器
filter()
使用现有数组中符合给定条件的元素创建新数组
因此,您可以轻松地使用它删除不符合条件的项。此函数的好处是,您可以在复杂数组上使用它,而不仅仅是字符串和数字
一些例子:
删除第一个元素:
function removeAnElement( array, index ) {
index--;
if ( index === -1 ) {
return array.shift();
} else {
return array.splice( index, 1 );
}
}
// Not very useful but it works
function removeFirst(element, index) {
return index > 0;
}
var arr = [1,2,3,5,6].filter(removeFirst); // [2,3,4,5,6]
function removeSecond(element, index) {
return index != 1;
}
var arr = [1,2,3,5,6].filter(removeSecond); // [1,3,4,5,6]
删除第二个元素:
function removeAnElement( array, index ) {
index--;
if ( index === -1 ) {
return array.shift();
} else {
return array.splice( index, 1 );
}
}
// Not very useful but it works
function removeFirst(element, index) {
return index > 0;
}
var arr = [1,2,3,5,6].filter(removeFirst); // [2,3,4,5,6]
function removeSecond(element, index) {
return index != 1;
}
var arr = [1,2,3,5,6].filter(removeSecond); // [1,3,4,5,6]
删除奇数元素:
function removeAnElement( array, index ) {
index--;
if ( index === -1 ) {
return array.shift();
} else {
return array.splice( index, 1 );
}
}
// Not very useful but it works
function removeFirst(element, index) {
return index > 0;
}
var arr = [1,2,3,5,6].filter(removeFirst); // [2,3,4,5,6]
function removeSecond(element, index) {
return index != 1;
}
var arr = [1,2,3,5,6].filter(removeSecond); // [1,3,4,5,6]
删除没有库存的项目
function removeOdd(element, index) {
return !(element % 2);
}
var arr = [1,2,3,5,6].filter(removeOdd); [2,4,6]
您可以将ES6解构分配功能与rest操作符一起使用。逗号表示要删除元素的位置,其余(…arr)运算符表示数组的其余元素
arr=arr.slice(1);
const source=[1,2,3,5,6];
函数removeFirst(列表){
var[,…arr]=列表;
返回arr;
}
const arr=removeFirst(源);
console.log(arr);//[2, 3, 5, 6]
console.log(源);//[1,2,3,5,6]
您也可以使用reduce执行此操作:
const inventory = [
{name: 'Apple', qty: 2},
{name: 'Banana', qty: 0},
{name: 'Orange', qty: 5}
];
const res = inventory.find( product => product.qty > 0);
不改变原始数组的Typescript解决方案
let arr = [1, 2, 3]
arr.reduce((xs, x, index) => {
if (index == 0) {
return xs
} else {
return xs.concat(x)
}
}, Array())
// Or if you like a oneliner
arr.reduce((xs, x, index) => index == 0 ? xs : xs.concat(x), Array())
函数removeElementAtIndex(输入:T[],索引:number){
返回input.slice(0,索引).concat(input.slice(索引+1));
}
从数组中删除元素有多种方法。让我在下面指出最常用的选项。我之所以写这个答案,是因为我找不到一个合适的理由从所有这些选项中使用什么。问题的答案是选项3(Splice())
1)SHIFT()-从原始数组中删除第一个元素并返回第一个元素
有关详细信息,请参阅参考资料。仅当您要删除第一个元素,并且仅当您同意更改原始数组时,才使用此选项
arr.slice(begin[,end])
function removeElementAtIndex<T>(input: T[], index: number) {
return input.slice(0, index).concat(input.slice(index + 1));
}
2)SLICE()-返回数组的副本,由开始索引和结束索引分隔
有关详细信息,请参阅参考资料。不能从此选项中删除特定元素。您只能对现有阵列进行切片,并获得阵列的连续部分。这就像从指定的索引中剪切数组一样。原始阵列不会受到影响
const array1 = [1, 2, 3];
const firstElement = array1.shift();
console.log(array1);
// expected output: Array [2, 3]
console.log(firstElement);
// expected output: 1
3)SPLICE()-通过删除或替换特定索引处的元素来更改数组内容。
有关详细信息,请参阅参考资料。splice()方法通过删除或替换现有元素和/或在适当位置添加新元素来更改数组的内容。返回更新的数组。原始数组得到更新
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]
console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]
接下来的问题是:
arr.splice(1,1)
对于第二个元素,我更喜欢splice()
而不是shift()
,因为它更灵活。我将来可能想做一些不同的事情,但我并不总是只想删除一个元素,甚至第一个元素。如果你来自谷歌:Gabriel更喜欢这个答案,但你应该知道还有shift(),请参阅Joseph的答案。这很有效,但只会删除数组中的第一个元素。@Gabriel:这不正是问题所在吗?我更喜欢shift()
而不是splice(…,…)
,因为它更显式、更直接。更正:它返回第一个元素,而不是一个新数组减去第一个元素,正如您提供的链接:)虽然代码示例是正确的,但描述是误导性的:(a)正如@DanH指出的,它是返回的第一个元素,并且(b) 输入数组被修改的事实并不清楚。因此,Ped的答案更为优越。shift
速度更快,除非你拼接了大量列表,并且大大改进了Joseph Silvashy的答案-描述和代码之间没有不一致。slice(start,end)
而不是“要删除多少”@Ped arr.unshift()不删除,但“将一个或多个元素添加到数组的开头”@seanjacob它是splice
不是slice
我猜是.array.splice(0,1)删除第一个元素。根据这里的每一个其他答案,以及根据。另外,为什么有人要通过-1来删除第一个元素?他们不会通过0吗?@Skypeckes我不能谈论第0个索引上拼接的有效性,但这里的索引在检查之前是递减的,所以传入时它应该是0。是的,它肯定可以移除第一个元素。我在这里测试了它:是的,没有变异。