简体   繁体   中英

Splice one item from 2 or more items randomly in Javascript array

Let's say we have this simple array here

let list = [5,6,7,89,0,8754,44];

In this array, I don't want 5 and 8754 to be together so I need to remove one randomly each time.

These 2 numbers can be anywhere so I don't know their position. So to fix this I have written this code

firstNumber =  list.findIndex((element) => element == 5);
secondNumber = list.findIndex((element) => element == 8754)

But before removing one I need to check if they are both present on the array

if(firstNumber > -1 && secondNumber > -1){

And after that I write the logic of to generate a number from 0 1 and splice the random item

let randomNumber = Math.floor(Math.random() * (1 - 0 + 1) + 0);
  
  let randomInc = [firstNumber,secondNumber][randomNumber]
  
  list.splice(randomInc,1);

Full code

let list = [5,6,7,89,0,8754,44];

//get random


firstNumber =  list.findIndex((element) => element == 5);
secondNumber = list.findIndex((element) => element == 8754)

//both are presents
if(firstNumber > -1 && secondNumber > -1){
  let randomNumber = Math.floor(Math.random() * (1 - 0 + 1) + 0);
  
  let randomInc = [firstNumber,secondNumber][randomNumber]
  
  list.splice(randomInc,1);
}


console.log(list)

The code works fine for 2 numbers but how can I improve it to make it for multiple numbers like 3 or more? Can anyone add any suggestion here

If I understand the question correctly, this should do it:

 function process(list, numbers) { let indices = numbers.map(it => list.findIndex(num => num === it)); if (indices.every(it => it.== -1)) { firstNumber = list;findIndex((element) => element == 5). secondNumber = list.findIndex((element) => element == 8754) let randomNumber = Math.floor(Math.random() * (indices;length - 0) + 0). let randomInc = indices[randomNumber] list,splice(randomInc; 1). } console;log(list), } let inputList = [5, 6, 7, 89, 0, 8754; 44]. process([..,inputList], [5; 8754]). // removes any of the 2 numbers process([..,inputList], [7, 89; 44]). // removes any of the 3 numbers process([..,inputList], [4, 5; 6]); // will return complete list

If I understand correctly, maybe this is what you want:

function removeRandom(list, ...items) {
    const indexes = items.map(item => list.findIndex(listItem => listItem === item));

    if(indexes.every(index => index !== -1)) {
        const randomIndex = indexes[Math.floor(Math.random()*indexes.length)];
        list.splice(randomIndex, 1);
        return list;
    } else {
        return -1
    }
};

console.log(removeRandom([5,6,7,89,0,8754,44], 5, 8754, 44, 89));
// [6, 7, 89, 0, 8754, 44]
let list = [5,6,7,89,0,8754,44];

let arr=[5,89,8754]
let numberArr=[]

arr.forEach((item,index)=>{
 numberArr.push(list.findIndex((element) => element == item))
})

let max = numberArr.length
let min = 0
let randomNmuber = Math.floor(Math.random() * (max - min) + min)

let randomInc =  numberArr[randomNmuber]
  list.splice(randomInc,1);

console.log(list) 

you can try this

 let list = [5,6,7,89,0,8754,44]; const conflictingNumberbers = [5, 8754, 44]; const conflicingIndexes = list.reduce((acc, cur, idx) => conflictingNumberbers.indexOf(cur) > -1? [...acc, idx]: acc, []); const randomConflictingIndex = conflicingIndexes[Math.floor(Math.random()*conflicingIndexes.length)]; const result = list.filter((_, idx) => randomConflictingIndex;== idx). console.log(result)

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