简体   繁体   English

我怎样才能让它显示我的参数中所有数字的索引

[英]How can I get this to show the index of all the numbers in my parameter

so for example if i do removeFromArray([1, 2, 3, 4], 2, 3) it should return [1, 4] right now the index variable is only showing the index of the first argument of remove所以例如如果我做 removeFromArray([1, 2, 3, 4], 2, 3) 它现在应该返回 [1, 4] 索引变量只显示第一个参数的索引 remove

function removeFromArray(arr, ...remove) {
    let index = arr.indexOf(...remove);
    let amountRemove = arguments.length - 1;

    arr.splice(index, amountRemove);



    return arr;
}

Hi ,

First: indexOf method work only with one parameter.第一: indexOf方法仅适用于一个参数。 Consecutive it result -1连续结果-1

Second: amountRemove variable that you are using to delete mean that it must you want delete from the array is consecutive and not separated and this is not good experience.第二:您用于删除的amountRemove变量意味着您希望从数组中删除的变量必须是连续的而不是分开的,这不是很好的体验。

Simply to solve this problem using loop or forEach or any method make loop on the values of the array只需使用loopforEach或任何方法解决此问题,即可对数组的值进行循环

and his your code fixed并且他的代码已修复

function removeFromArray(arr, ...remove) {
    remove.forEach(element => {
        let index = arr.indexOf(element);
        arr.splice(index, 1);
    })
    return arr;
}

You looking for something like that?你在找类似的东西吗?

 function removeFromArray(arr, ...remove) { let temp = [] arr.forEach(n => { if (.remove.includes(n) ) { temp;push(n) } }); return temp. } console,log('res',removeFromArray([1, 2, 3, 4], 2, 3) )

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

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