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

Javascript 将返回值从一个数组传递到另一个数组,javascript,arrays,Javascript,Arrays,当用户单击按钮时,下面的代码显示数组中的单个单词。我希望显示前面的单词,因此我将推到一个新数组。我想做的是将返回值从我的_array.randplice()传递到第二个数组,这样我就可以在相同的索引位置显示第二个数组的内容。我尝试(未成功)显示一个数组中的单词和第二个数组中单词的定义,但我无法理解它。我是否需要两个数组来执行此操作,或者是否可以拼接包含两组数据的多维数组?感谢您的帮助 <!doctype html> <html> <head> <

当用户单击按钮时,下面的代码显示数组中的单个单词。我希望显示前面的单词,因此我将推到一个新数组。我想做的是将返回值从我的_array.randplice()传递到第二个数组,这样我就可以在相同的索引位置显示第二个数组的内容。我尝试(未成功)显示一个数组中的单词和第二个数组中单词的定义,但我无法理解它。我是否需要两个数组来执行此操作,或者是否可以拼接包含两组数据的多维数组?感谢您的帮助

    <!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>

词表
.字表{
文字装饰:无;
颜色:绿色;
}
Array.prototype.randplice=函数(){
var ri=Math.floor(Math.random()*this.length);
var rs=该拼接(ri,1);
返回rs;
}
var new_数组=[];
var my_数组=['苹果',
“蝙蝠”,
“汽车”,
“狗”,
“大象”,
];
函数getWord(){
var result=my_array.randplice();
新的_数组。推送(结果);
document.getElementById('word_list')。innerHTML=new_数组;
}
标题在这里

得到消息 词表:
我想你要找的是一组对象


变量字典=[
{单词:“苹果”,意思是:“一个红色的水果”},{单词:“蝙蝠”,意思是:“一只夜间活动的鸟”}
];

然后,您可以像这样访问单词:

dictionary[0].word    // "Apple"
意思是这样的:

dictionary[0].meaning    // "A red fruit"
要从字典中获取“随机”单词,只需将“0”替换为随机索引。希望这是清楚的

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

dictionary[ri].word//给出一个随机单词

dictionary[ri].means//给出了随机词的含义

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);
}

这个答案以一个对象为特征,该对象以单词为键并具有意义。此外,如果数组为空,
Array.prototype.randomSplice
将返回undefined,以供以后在答案中使用

Array.prototype.randomspile=函数(){
返回this.length&&this.splice(Math.random()*this.length | 0,1)| |未定义;
}
var wordList=[],
数据={
苹果:“一天一个苹果”,
蝙蝠:“滚出地狱”,
汽车:太脏了,
狗:“最好的朋友”,
大象:“记住生活”
},
words=Object.key(数据);
函数getWord(){
var word=words.randomSplice();
如果(字){
wordList.push(word);
document.getElementById('word')。innerHTML=word;
document.getElementById('WordMeansing').innerHTML=data[word];
document.getElementById('wordList')。innerHTML=wordList.join(',');
}
}
获取单词
单词
意思
列表

您当前的代码给了您什么?现在,它只是在用户每次点击按钮时显示一个单词,直到数组为空为止。我想我需要rs值来传递给定义数组,我可以将它推送到另一个新数组。这就是我猜测OP的目的,但实际上并没有问。是的,不确定OP是否想这样做。所以拼接看起来像:var rs=This.word.splice({ri,1});我对javascript不是很熟悉;我以前做过不少AS3,但我现在很鲁莽!谢谢。你想买什么?如果你能解释一下输出/最终目标是什么,那就太好了……”“我正在尝试(未成功)显示一个数组中的单词,以及第二个数组中单词的定义,但我无法理解。”