如何使用同样位于对象中的 "name" 的关键字将对象的 "id" 记录在 json 文件中?

How do I log the "id" of an object in a json file using a keyword of the "name" also located in the object?

我通过在终端中输入“node app.js”来启动我的程序。我的总体目标是在终端中再次输入一个特定的词,例如“pinball”。然后程序将继续 运行 并在 json url 中搜索单词“pinball”。 json url 的其中一个对象内有名称:“Supreme®/Stern® Pinball Machine”。它将使用单词“pinball”定位包含该名称的对象,并在控制台日志中仅记录也在该对象内的 id。下面是我说的 json 的一部分:

{
    "unique_image_url_prefixes": [],
    "products_and_categories": {
        "Accessories": [{
            "name": "Supreme®/Stern® Pinball Machine",
            "id": 171495

        }]
    }
}

json 的完整 url 可在以下位置找到:http://www.supremenewyork.com/mobile_stock.json 任何帮助都很棒,我能够澄清任何没有意义的事情。我计划对 json 文件中的所有 ID 执行此操作,但是一个关于如何执行此操作的示例对于编写其他代码非常有用。这是我一直在处理的代码部分:

const request = require('request')
request('http://www.supremenewyork.com/mobile_stock.json', function(error, response, body) {
    const ids = Object.values(JSON.parse(body).products_and_categories).reduce((o, items) => o.concat(items.map(({
         Id
    }) => id)), [])
    console.log(ids)
})

简而言之,我想 运行 我的程序,在终端中输入 "pinball" 这个词,然后它将控制台记录“171495”

据我在数据中看到的那样,该对象包含键,并且在键中您有一个数组,其中包含特定类别的所有产品。这可能有效:

const request = require('request');

request('http://www.supremenewyork.com/mobile_stock.json', function(error, response, body) {

    const searchword = "Pinball";

    const data = JSON.parse(body).products_and_categories;

    const ids = [];

    for (var key in data) {
        if (data.hasOwnProperty(key)) {
            data[key].map(item => {
                if (item.name.indexOf(searchword) >= 0) {
                    ids.push(item.id);
                }
            });
        }
    }

    console.log(ids);
})