首页 > 解决方案 > 获取 json 数据时使用外部字符串时遇到问题

问题描述

我目前正在尝试使用字符串“region”在我的 JSON 文件中查找 JSON 的特定部分。

前任。配置区域。${region}等于 config.regions.northeast(假设 hooks[i].regions[c] == 东北)

        for (var c = 0; c < hooks[i].regions.length; c++) {
            let region = JSON.stringify(hooks[i].regions[c]);

            if (config.regions.`${region}`.includes(city)) {
                webhook = config.hooks[i].webhook;
                post(webhook, title, price, link, img, city, keyword);

            }
        }

标签: node.jsjson

解决方案


如果我理解你的问题。我相信您应该使用数组语法来访问您的对象:

for (var c = 0; c < hooks[i].regions.length; c++) {
        let region = JSON.stringify(hooks[i].regions[c]);

        if (config.regions[region].includes(city)) {
            webhook = config.hooks[i].webhook;
            post(webhook, title, price, link, img, city, keyword);

        }
    }

推荐阅读