Javascript 使用切片从数组中删除元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11848534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove element from array, using slice
提问by MartinElvar
I am trying to remove a element from my array using slice, but i can't get it to work, look at this piece of code.
我正在尝试使用切片从我的数组中删除一个元素,但我无法让它工作,看看这段代码。
console.log(this.activeEffects); // Prints my array
console.log(this.activeEffects.slice(0,1)); // Remove from index 0, and remove one.
console.log(this.activeEffects); // Prints array again, this time my element should be gone
Result of this is.
这样做的结果是。
So what is get from this is, at first the array is whole, as it should be. Then its prints what is sliced of the array. Finally the third should be empty? or?
所以从这里得到的是,首先数组是完整的,它应该是。然后它打印出数组的切片内容。最后第三个应该是空的?或者?
回答by TSL
I believe you're looking for splice
. From W3 Schools:
我相信你正在寻找splice
。来自W3 学校:
The splice() method adds/removes items to/from an array, and returns the removed item(s).
splice() 方法向/从数组添加/删除项目,并返回删除的项目。
Take a look at the example on that page; the use case there is similar to what you want to achieve.
看看那个页面上的例子;那里的用例与您想要实现的类似。
EDIT: Alternative link to MDN, as suggested by Nicosunshine; much more information about the command there.
编辑:按照 Nicosunshine 的建议,指向MDN 的替代链接;有关该命令的更多信息。
回答by corlaez
function removeItemWithSlice(index) {
return [...items.slice(0, index), ...items.slice(index + 1)]
}
Slice will create a new array. We create two arrays: from beggining to index and from index+1 to end. Then we apply the spread operator (...) to take the items of those arrays and create a new single array containing all the items we care. I will paste an equivalent way if you don't like the one liner:
Slice 将创建一个新数组。我们创建了两个数组:从开始到索引和从索引+1 到结束。然后我们应用扩展运算符 (...) 来获取这些数组的项目,并创建一个包含我们关心的所有项目的新单个数组。如果你不喜欢一个班轮,我会粘贴一个等效的方式:
function removeItemWithSlice(index) {
const firstArr = items.slice(0, index);
const secondArr = items.slice(index + 1);
return [...firstArr , ...secondArr]
}
回答by spirinvladimir
a.slice(0, index).concat(a.slice(index + 1))
回答by Esailija
.slice
does not mutate the array, you could use .splice()
to remove the item at index i
in the array:
.slice
不会改变数组,您可以使用.splice()
删除i
数组中索引处的项目:
this.activeEffects.splice(i, 1)
回答by Chacal
This is what I was able to come up with :
这是我能够想出的:
var newArray = oldArray.slice(indexOfElementToRemove+1).concat(oldArray.slice(0,indexOfElementToRemove));
回答by canon
Array.prototype.slice()
...
Array.prototype.slice()
...
does not alter the original array, but returns a new "one level deep" copy that contains copies of the elements sliced from the original array. Elements of the original array are copied into the new array as follows:
不会改变原始数组,而是返回一个新的“一级深”副本,其中包含从原始数组中切片的元素的副本。原始数组的元素被复制到新数组中,如下所示:
Whereas Array.prototype.splice()
...
而...Array.prototype.splice()
Changes the content of an array, adding new elements while removing old elements.
更改数组的内容,在删除旧元素的同时添加新元素。
This example should illustrate the difference.
这个例子应该说明差异。
// sample array
var list = ["a","b","c","d"];
// slice returns a new array
console.log("copied items: %o", list.slice(2));
// but leaves list itself unchanged
console.log("list: %o", list);
// splice modifies the array and returns a list of the removed items
console.log("removed items: %o", list.splice(2));
// list has changed
console.log("list: %o", list);
回答by BlackMario
Look at here : http://www.w3schools.com/jsref/jsref_slice_array.asp
看看这里:http: //www.w3schools.com/jsref/jsref_slice_array.asp
You can see that the slice method select object et throw them into a new array object ^^ So you can't delete an object like this, may be you can try a thing like this :
你可以看到 slice 方法 select object et throw them into a new array object ^^ 所以你不能像这样删除一个对象,也许你可以尝试这样的事情:
var a = ["a","b","c"]; (pseudo code)
/* I wan't to remove the "b" object */
var result = a.slice(0,1)+a.slice(2,1); /* If you considers that "+" is a concatenation operator, i don't remember if it is true... */