2016-10-15 11 views
0

Я создаю генератор случайных чисел. Я не могу понять, как удалить уже показанную цитату из массива, чтобы она не отображалась дважды при нажатии кнопки. Какие-либо предложения? Вот javascript:Удалить найденный элемент из списка массивов

var quoteArray = [ 
     { 
      content: "quote 1 goes in here", 
      author: "author 1" 
     }, 
     { 
      content: "quote 2 goes in here", 
      author: "author 2" 
     } 
     ]; 

    var button = document.getElementById('quote-button'), 
     quote = document.getElementById('quote'), 
     author = document.getElementById('quote-author'), 
     random; 

window.onload = randomQuote; 
button.addEventListener('click', randomQuote); 

function randomQuote(){ 
    random = Math.floor(Math.random() * quoteArray.length); 
    quote.innerHTML = quoteArray[random].content; 
    author.innerHTML = '— ' + quoteArray[random].author; 
} 

Заранее спасибо!

+0

Вы можете удалить элемент с индексом 'i' с функцией сплайсинга как таковой:' Array.splice (I, 1); ' – ahitt6345

ответ

0

Вы можете использовать функцию Array.prototype.splice(start, count).

Например:

var array = [1, 2, 3, 4, 5] 
// remove 2 in the array. 
var index = 1; 
array.splice(1, 1); 
// array = [1, 3, 4, 5] 

 Смежные вопросы

  • Нет связанных вопросов^_^