I have the following task - there is an array of some items. I use random, get item number, write item's value in div and delete element with item number from array. e.g.
array = (1,2,3)
random = 2
div = 3
delete from array = (1,2)
looping, etc
So I wrote the following script:
window.onload = function () {
var verb_array = new Array (1,2,3)
var random_verb_array = Math.floor(Math.random() * (verb_array.length - 0+0)) + 0;
var random_verb_array_2 = verb_array[random_verb_array];
var show = document.getElementById("wuza").innerHTML = random_verb_array_2;
delete verb_array[random_verb_array];
var test = document.getElementById("huza").innerHTML = '<Br><Br>' + verb_array
}
<!DOCTYPE html>
<div id="wuza"></div>
<div id="huza"></div>
-
Refer http://stackoverflow.com/questions/281264/remove-empty-elements-from-an-array-in-javascriptGarvit Mangal– Garvit Mangal2016年05月25日 20:11:37 +00:00Commented May 25, 2016 at 20:11
1 Answer 1
You're probably looking to use splice.
verb_array.splice(random_verb_array, 1);
So assuming random_verb_array is the random index you've come up with, it'll remove 1 (second parameter) from the given index (first parameter).
Sign up to request clarification or add additional context in comments.
Comments
lang-js