Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
51 views
in Technique[技术] by (71.8m points)

javascript - How to Splice method performance error with max

I have a dilemma with this line of code

let numero = [11,5,7,9,10,12];
console.log(numero.splice(Math.min.aply(null, numero), 1));
console.log(numero);

This line of code should eliminate the number 5 which is the smallest of the array, but it eliminates the 12, I have tried in different ways, but I can't get it to eliminate the 5. I have not found the error thank you all.

question from:https://stackoverflow.com/questions/65865563/how-to-splice-method-performance-error-with-max

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You found the number, but then you have tried to remove it by itsef instead of by its index, which is what splice was expecting. Try this:

numero.splice(numero.indexOf(Math.min(...numero)),1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...