Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何添加对象的输出';将函数转换为数组_Javascript_Html_Css_Reactjs - Fatal编程技术网

Javascript 如何添加对象的输出';将函数转换为数组

Javascript 如何添加对象的输出';将函数转换为数组,javascript,html,css,reactjs,Javascript,Html,Css,Reactjs,这很难用语言表达,但我想得到存储在对象中的函数的返回值,将该值添加到数组中并输出它。我已经做了所有的工作,但它不会改变再次运行,所以说它输出678的随机数,下次它也会输出相同的数字,以此类推。我在react中创建了这个很酷的终端,它获取键的命令(如果它存在),并通过返回jsx在控制台中输出它 这是我的文件结构 这是我的代码。。。(我遇到了格式问题,这是下面代码的更好版本) 编辑:尝试使用以下任何一项都不会返回任何结果 rand: () => ( "x" ), 或 下面是正在发生

这很难用语言表达,但我想得到存储在对象中的函数的返回值,将该值添加到数组中并输出它。我已经做了所有的工作,但它不会改变再次运行,所以说它输出678的随机数,下次它也会输出相同的数字,以此类推。我在react中创建了这个很酷的终端,它获取键的命令(如果它存在),并通过返回jsx在控制台中输出它

这是我的文件结构

这是我的代码。。。(我遇到了格式问题,这是下面代码的更好版本)

编辑:尝试使用以下任何一项都不会返回任何结果

rand: () => (
    "x"
),

下面是正在发生的事情的演示

我键入命令。。。

我得到我的输出,这是很好的。。。

但从那时起,重复相同的命令不会生成新的随机数,我将继续重复第一个返回值


我非常感谢您的帮助,但请记住,我仍在学习react和JavaScript,希望听到任何建设性的反馈。如果需要,我也会继续更新信息。谢谢

发现了什么问题,我调用了没有括号的函数。我必须将命令文件更改为以下内容

const Commands = {
help: "plunketTheTerminal is a online terminal for fun, it contains lots of different functions and commands. In fact you can SUBMIT YOUR OWN COMMANDS at our github if you are good with computers :).",
rand: () => {
    return Math.floor(Math.random() * 1000 ) + 1;
},
//"rand --info": "Will generate a random number between 1 and 1000",

}
我不得不改变路线

this.outputs.unshift(this.commands[this.state.inputValue]);

愚蠢的错误,希望这能帮助有类似问题的人,尽管我认为这不太可能

rand: () => {
    return "x"
},
const Commands = {
help: "plunketTheTerminal is a online terminal for fun, it contains lots of different functions and commands. In fact you can SUBMIT YOUR OWN COMMANDS at our github if you are good with computers :).",
rand: () => {
    return Math.floor(Math.random() * 1000 ) + 1;
},
//"rand --info": "Will generate a random number between 1 and 1000",

}
this.outputs.unshift(this.commands[this.state.inputValue]);
this.outputs.unshift(this.commands[this.state.inputValue]());