简体   繁体   English

array.splice() 返回我想要消除的项目而不是数组减去项目

[英]array.splice() returns the item I want to eliminate rather than the array minus the item

I'm trying to remove an item from an array using the indexOf() with splice() technique suggested.我正在尝试使用indexOf()和建议的splice()技术从数组中删除一个项目。 This is what's happening这就是正在发生的事情

let someArray: string[] = [first, second, third, fourth, fifth, sixth];
let newArray: string[] = someArray.splice(3, 1);

console.log(newArray);

//deisred result = [first, second, third, fifth, sixth]

//result I'm getting = [fourth]

That's not what virtually every article I've come across says should happen.这并不是我遇到的几乎每篇文章都应该发生的事情。 Can someone shed light on this?有人可以解释一下吗?

UPDATE I discovered this problem in my code when I was only ghetting 1 result where I was expecting more and tracked it back to this point.更新我在我的代码中发现了这个问题,当时我只得到 1 个结果,我期待更多,并将其追溯到这一点。

Because when you splice an array you are mutating it, which means you are changing the original array.因为当您拼接一个数组时,您正在对其进行变异,这意味着您正在更改原始数组。 You're storing the result (the element you're splicing from the array) within the "newArray" variable that you have created here.您将结果(您从数组中拼接的元素)存储在您在此处创建的“newArray”变量中。 So this:所以这:

var arr = [1, 2, 3, 4];

var mine = arr.splice(1, 1);
console.log(mine);
console.log(arr);

would return the original ray minus index one if we print arr to the console, and will return [2] if we print mine to the console.如果我们将 arr 打印到控制台,将返回原始光线减去索引 1,如果我们将我的打印到控制台,将返回 [2]。 To get the output you're expecting, you would have to perform a different operation such as iterating through the array and utilizing splice differently.要获得您期望的输出,您必须执行不同的操作,例如遍历数组并以不同方式利用 splice。 Here is an example:下面是一个例子:

var arr = [1, 2, 3, 4];

var mine = [];

for(var i = 0; i < arr.length; i++) {
    if(i !== 3) {
        mine.push(arr[i]);
    }
}

Now I am not mutating the original array, and I am simply pushing the elements to a new array.现在我没有改变原始数组,我只是将元素推送到新数组。

But if you want to simply mutate the original array and not store the new array in some sort of variable you can simply splice the original array:但是如果你想简单地改变原始数组而不是将新数组存储在某种变量中,你可以简单地拼接原始数组:

var arr = [1, 2, 3, 4];

arr.splice(3, 1);
console.log(arr);

However, if you are passing it to a function, i'd probably not mutate an array outside of the function, and i'd simply return a value and store that value in a new variable:但是,如果你将它传递给一个函数,我可能不会在函数之外改变一个数组,我只会返回一个值并将该值存储在一个新变量中:

var arr = [1, 2, 3, 4];


function deleteIndex(ar, i) {
    var a = [];
    ar.forEach(function(elt, index) {
        if(index === i) {

        }
        else {
            a.push(elt);
        }
    });
    return a;
}

var newArr = deleteIndex(arr, 3);
console.log(newArr);

This way you can delete any index, or pass a function and criteria that you would want to use to determine if an index should be deleted, without changing to top-level structure of your original array by utilizing functional programming.通过这种方式,您可以删除任何索引,或传递一个函数和条件,您将希望使用这些函数和条件来确定是否应删除索引,而无需使用函数式编程更改原始数组的顶级结构。 There are also some function in the underscore module that can help you if that's the case.如果是这种情况,下划线模块中还有一些功能可以帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM