如何在 for 循环中提取 JSON 文件的元素

Posted

技术标签:

【中文标题】如何在 for 循环中提取 JSON 文件的元素【英文标题】:How to pull elements of JSON files in a for loop 【发布时间】:2021-06-08 17:54:46 【问题描述】:

我正在为我在 discord.js 中的一个项目构建扩展系统,我需要从用户放置在 ./modules 目录中的 JSON 文件中提取内容。这些文件可以有任何名称,并且应该是这样的结构:

./modules/module_name.json:


    "name": "(module name)",
    "author": "(author name)",
    "commands": 
        "command1": "(url_of_command)/command1.js",
        "command2": "(url_of_command)/command2.js",
        etc...etc...
    

我当前的代码如下所示:

./index.js:

/*

Toggle - Command indexer

Original code by Anden Wieseler and ZedTek. For Licensing info, see https://github.com/ZedTek-Official/toggle-base/blob/main/LICENSE

*/

// Tell console that we're ready

console.log('Toggle v1 - STARTED');

// Load dependencies and get global vars

const http = require('http');
const fs = require("fs")
const db = require("quick.db")
const Discord = require('discord.js');
const global = require('./globalTMP.json');
const client = new Discord.Client();
const prefix = global.info.prefix;

// Kickoff

client.on("ready", () =>
    client.user.setPresence(
        game: 
            name: global.info.game.name,
            type: global.info.game.type
        
    );
    loadExt();
);

function loadExt()
    const moduleFiles = fs.readdirSync(`./modules`).filter(file => file.endsWith('.json'));
    for (const module of moduleFiles)
        const mod = require(`./modules/$module`)
        if (db.get('parsedModules') === module)
            return;
        
        else
            for (const command of mod.commands)
                download(command, `./commands/ext/$module.name/$command`, error);
            
            db.add('parsedModules', module)
        
        
        
    


// Discord API login

client.login(global.token);

^^ 我已经省略了与这个问题无关的代码。

当我运行代码时,我收到一条错误消息,指出 mod.commands isn't iterable。非常感谢您的帮助。

【问题讨论】:

你确定 mod.commands 是你认为的那样吗?你确认你得到了你所期望的吗?该错误似乎很明显,您正在尝试迭代不可迭代的东西。记录 mod.commands 的值并验证您正在迭代您的想法。 @basic,它按预期打印出commands的数组 【参考方案1】:

问题是你不能在一个对象上运行for (const command of mod.commands),你必须使用for (let command in mod.commands)。在这种情况下,command 将是关键,因此您需要使用 mod.commands[command],而不是代码中当前的 command

例子:

for (let command in mod.commands)
                download(mod.commands[command], `./commands/ext/$module.name/$mod.commands[command]`, error);
            

【讨论】:

【参考方案2】:

这是因为 commands 不是数组而是一个对象,而每个命令都是该对象上的一个属性。

你可以这样做:

const data = 
    "name": "(module name)",
    "author": "(author name)",
    "commands": 
        "command1": "(url_of_command)/command1.js",
        "command2": "(url_of_command)/command2.js",
    


const commands = Object.keys(data.commands);
for (let commandAtt of commands) 
  const cmd = data.commands[commandAtt];
    console.log(cmd);

或者

const data = 
    "name": "(module name)",
    "author": "(author name)",
    "commands": 
        "command1": "(url_of_command)/command1.js",
        "command2": "(url_of_command)/command2.js",
    


for (let commandAtt in data.commands) 
  const cmd = data.commands[commandAtt];
  console.log(cmd);

【讨论】:

以上是关于如何在 for 循环中提取 JSON 文件的元素的主要内容,如果未能解决你的问题,请参考以下文章

我如何在F#中制作一个for循环来扫描JSON。

如何在批处理文件的for循环中提取子字符串[关闭]

R在for循环中从data.table中提取元素[重复]

如何提取for循环的主体? [复制]

如何在 Python 中没有任何循环(例如:for、while 等)递归 JSON 文件

使用for循环从数组迭代到列表