简体   繁体   中英

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

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. Consecutive it result -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.

Simply to solve this problem using loop or forEach or any method make loop on the values of the array

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) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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