简体   繁体   English

将返回值从一个数组传递到另一个数组

[英]Passing a return value from one array to another array

The code below displays individual words from an array when a user clicks the button. 当用户单击按钮时,下面的代码显示来自数组的单个单词。 I want the previous words to display so I am pushing to a new array. 我希望显示以前的单词,所以我要推送到一个新数组。 What I would like to do is pass the return value from my_array.randsplice() to a second array so I can display the contents of the second array at the same index position. 我想做的是将my_array.randsplice()的返回值传递给第二个数组,这样我就可以在同一索引位置显示第二个数组的内容。 What I am trying (unsuccessfully) to do is show a word from one array and the definition of the word from the second array but I cant figure it out. 我正在尝试(未成功)执行的操作是显示一个数组中的单词以及第二个数组中单词的定义,但我无法弄清楚。 Do I need two arrays to do this or is is possible to splice a multi-dimensional array that contains both sets of data? 我是否需要两个数组来执行此操作,或者可以拼接包含两个数据集的多维数组? Any help is appreciated. 任何帮助表示赞赏。

    <!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Word List</title>
<style>
.wordList {
    text-decoration: none;
    color: green;
}
</style>
<script>
Array.prototype.randsplice = function(){
    var ri = Math.floor(Math.random() * this.length);
    var rs = this.splice(ri, 1);
    return rs;  
}
var new_array =[];
var my_array = [' Apple',
        ' Bat',
        ' Car',
        ' Dog',
        ' Elephant',
        ];


function getWord() {
    var result = my_array.randsplice();
    new_array.push(result);
    document.getElementById('word_list').innerHTML = new_array;
}

</script>
</head>
<body>
<div id="main">

          <div class="header">
            <h1 id="game_title">Title Here</h1> 
            <hr>
            <button onclick="getWord()">Get Word</button>
            <h2 id="word_list_title">Word List:</h2>
            <h2 id="word_list" class="wordList"> </h2>
          </div>
</div>

</body>
</html>

I think what you are looking for is an array of objects. 我认为您正在寻找的是一组对象。

var dictionary = [ {word: "Apple", meaning: "A red fruit"}, {word: "Bat", meaning: "A nocturnal bird"} ];

You can then access the word like so: 然后,您可以像这样访问单词:

dictionary[0].word    // "Apple"

And the meaning like this: 意思是这样的:

dictionary[0].meaning    // "A red fruit"

All you need to do to get a "random" word from the dictionary is to replace "0" with the random index. 要从字典中获取“随机”字,您需要做的就是用随机索引替换“ 0”。 Hope this is clear! 希望这很清楚!

var ri = Math.floor(Math.random()*dictionary.length);

dictionary[ri].word // Gives a random word

dictionary[ri].meaning // Gives that random words meaning

function getWord() {
var ri = Math.floor(Math.random()*dictionary.length);
document.getElementById('word_list').innerHTML = dictionary[ri].word + ":" + dictionary[ri].meaning;
dictionary.splice(ri, 1);
}

This answer features an object with words as key and a meaning. 这个答案的特征是一个以单词为键和含义的对象。 Additionally, the Array.prototype.randomSplice returns undefined if the array is empty, for later use as in the answer. 此外,如果数组为空,则Array.prototype.randomSplice返回undefined,供以后在答案中使用。

 Array.prototype.randomSplice = function () { return this.length && this.splice(Math.random() * this.length | 0, 1) || undefined; } var wordList = [], data = { Apple: 'An apple a day', Bat: 'Out of hell', Car: 'So dirty', Dog: 'The best friend', Elephant: 'Remember the life' }, words = Object.keys(data); function getWord() { var word = words.randomSplice(); if (word) { wordList.push(word); document.getElementById('word').innerHTML = word; document.getElementById('wordMeaning').innerHTML = data[word]; document.getElementById('wordList').innerHTML = wordList.join(', '); } } 
 <button onclick="getWord();">Get Word</button> <h3>Word</h3> <div id="word"></div> <h3>Meaning</h3> <div id="wordMeaning"></div> <h3>List</h3> <div id="wordList"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM