JavaScript array splice

前端 未结 2 1742
长情又很酷
长情又很酷 2021-01-23 04:56

Hello StackOverflow community!

At the moment I am completly stuck with my code, have try different ways to remove a value from array when picked, I know I have to use sp

相关标签:
2条回答
  • 2021-01-23 05:48

    You can use splice() to remove an item from the array, since splice returns an array then you can access the element at the index 0 to get the fetched src value

    '<div class="back"><img src="./kaart/' + cars.splice(Math.floor(Math.random() * cars.length),1)[0] + '.png"</img> </div>'
    
    0 讨论(0)
  • 2021-01-23 05:54

    First, put the random-number in a seperate variable

    var rnd = Math.floor(Math.random()*cars.length);
    var rand = cars[rnd];
    

    Then remove that specific item from the array

    cars.splice(rnd, 1);
    

    Cheers R

    0 讨论(0)
提交回复
热议问题