【问题标题】:How to write for loop to take property from object and insert it in function javascript如何编写for循环以从对象中获取属性并将其插入函数javascript
【发布时间】:2020-10-13 11:47:40
【问题描述】:

我是编程初学者,希望有人帮助我编写此代码。

我的代码是:-

var i = Object.keys(mws.Result.InventorySupplyList.member).length
console.log(i)
a0 = mws.Result.InventorySupplyList.member[0].ASIN
a1 = mws.Result.InventorySupplyList.member[1]
const amazonScraper = require('amazon-buddy');
(async () => {
    try {
        // Get single product details by using ASIN id
        const product_by_asin_0 = await amazonScraper.asin({ asin: a0 });
        console.log(product_by_asin_0)
    } catch (error) {
        console.log(error);
    }
})();
(async () => {
    try {
       // Get single product details by using ASIN id
        const product_by_asin_1 = await amazonScraper.asin({ asin: a0 });
        console.log(product_by_asin_1)
    } catch (error) {
        console.log(error);
    }
})();

mws 是 JOSN 对象

const mws = {
    "ResponseMetadata": {
      "RequestId": "e8698ffa-8e59-11df-9acb-230ae7a8b736"
    },
    "Result": {
      "InventorySupplyList": {
        "member": [
          {
            "SellerSKU": "SampleSKU1",
            "ASIN": "B00000K3CQ",
            "TotalSupplyQuantity": "20",
            "FNSKU": "X0000000FM",
            "Condition": "NewItem",
            "SupplyDetail": {},
            "InStockSupplyQuantity": "15",
            "EarliestAvailability": {
              "TimepointType": "Immediately"
            }
          },
          {
            "SellerSKU": "SampleSKU2",
            "ASIN": "B00004RWQR",
            "TotalSupplyQuantity": "0",
            "FNSKU": "X00008FZR1",
            "Condition": "UsedLikeNew",
            "SupplyDetail": {},
            "InStockSupplyQuantity": "0"
          }
        ]
      }
    },
    "Headers": {
      "x-mws-quota-max": "",
      "x-mws-quota-remaining": "",
      "x-mws-quota-resetson": "",
      "x-mws-timestamp": "2018-09-05T06:13:00.276Z",
      "content-type": "text/xml",
      "content-charset": "",
      "content-length": "",
      "content-md5": "",
      "date": ""
    },
    "StatusCode": 200
  }

我想从 mws 对象中获取 ASIN 并将其插入到其他代码中,该代码将从 amazon.com 抓取详细信息,因为您已经注意到我发现对象 mws 有 2 个 ASIN 并手动将其插入刮刀。如果您能帮助我如何编写 for 循环以在任何 ASIN 中执行代码。

【问题讨论】:

    标签: javascript node.js loops


    【解决方案1】:
    const Promise = require('bluebird');
    const amazonScraper = require('amazon-buddy');
    const requests = Promise.all(Object.keys(mws["Result"]["InventorySupplyList"]["member"])
      .map(member => amazonScraper.asin({ asin: member["ASIN"] }));)
    requests.then(responces => console.log(responces))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-15
      • 2018-11-02
      • 1970-01-01
      相关资源
      最近更新 更多