我想知道是否可以在json文件中使用switch语句

时间:2019-07-16 00:09:48

标签: javascript json discord discord.js

所以我想让index.js看起来更整洁,所以我认为如果命令(switch语句)在另一个文件中会更整洁,所以现在我想知道是否可以将它们放在json文件中,或者(如果不可能的话)插入另一个js文件,然后以某种方式将其导入index.js

1 个答案:

答案 0 :(得分:1)

您不能在JSON文件中执行此操作,但可以在js文件中进行操作。

这里是一个例子:

1-创建一个名为helper.js的文件

2-在其中添加以下代码:

module.exports = {
// example function
biggerThanFive: array => {
    return array.filter(elem => {
        return elem > 5;
    });
}

}

3-在index.js文件中,需要这样的帮助程序文件:

const helpers = require("./helper");

4-调用该函数并为其指定一个值:

helper.biggerThanFive([1, 2, 3, 4, 5, 6, 7, 8, 9])
// [6, 7, 8, 9]
相关问题