【问题标题】:How to add dynamic object as a nested object to another object - javascript如何将动态对象作为嵌套对象添加到另一个对象 - javascript
【发布时间】:2021-09-16 22:39:23
【问题描述】:

我正在从 API 获取数据并将其构造为常量 res

现在使用嵌套的 for 循环来获取设备和国家/地区,创建一个添加这些名称的变量,然后要将具有设备名称和国家/地区名称的所有数据添加到另一个对象。

我想要实现的输出应该是这样的:

{
  "GEO_Device": {
    "masterID": {
      "appID": "1490262670",
      "cvr": "51.06666666666667",
      "name": "Hard Rock Blackjack",
      "anchor": "Hard Rock Blackjack",
      "short_desc": "Complete Task to Earn!",
      "long_desc": "Discover the world of online Hard Rock Casino games!",
      "revenue": "0.45",
      "image": "https://cdn.adgem.com/campaigns/12119/campaign-offerwall-creatives/icons/202106252106.jpg",
      "geo": [
        "DE",
        "FR",
        "FI",
        "DK",
        "CZ",
        "GB",
        "CH",
        "SE",
        "ES",
        "PT",
        "NO",
        "NL",
        "HU",
        "IT",
        "GR"
      ],
      "masterID": "adGem12119",
      "devices": [
        "ipad",
        "iphone"
      ],
      "link": "https://api.adgem.com/v1/click?all=1&appid=4746&cid=12119&playerid={playerid}",
      "reward": "nothing",
      "source": "adgem",
      "isPromoted": true,
      "priority": "1",
      "isDisabled": false,
      "forceType": "null"
    },
    "masterID2": {
      "appID": "1490262670",
      "cvr": "51.06666666666667",
      "name": "Hard Rock Blackjack",
      "anchor": "Hard Rock Blackjack",
      "short_desc": "Complete Task to Earn!",
      "long_desc": "Discover the world of online Hard Rock Casino games!",
      "revenue": "0.45",
      "image": "https://cdn.adgem.com/campaigns/12119/campaign-offerwall-creatives/icons/202106252106.jpg",
      "geo": [
        "DE",
        "FR",
        "FI",
        "DK",
        "CZ",
        "GB",
        "CH",
        "SE",
        "ES",
        "PT",
        "NO",
        "NL",
        "HU",
        "IT",
        "GR"
      ],
      "masterID": "adGem12119",
      "devices": [
        "ipad",
        "iphone"
      ],
      "link": "https://api.adgem.com/v1/click?all=1&appid=4746&cid=12119&playerid={playerid}",
      "reward": "nothing",
      "source": "adgem",
      "isPromoted": true,
      "priority": 1,
      "isDisabled": false,
      "forceType": "null"
    }
  }
}

但不是那样,我只会将一个数据添加到对象中,而其他数据会被覆盖。见下文:

{
    "GEO_DeviceName": {
        "appID": "uu32mdnnsjns",
        "cvr": 51.06666666666667,
        "name": "Hard Rock Blackjack",
        "anchor": "Hard Rock Blackjack",
        "short_desc": "Complete Task to Earn!",
        "long_desc": "Discover the world of online Hard Rock Casino games!",
        "revenue": 0.45,
        "image": "https://cdn.adgem.com/campaigns/12119/campaign-offerwall-creatives/icons/202106252106.jpg",
        "geo": [
            "DE",
            "FR",
            "FI",
            "DK",
            "CZ",
            "GB",
            "CH",
            "SE",
            "ES",
            "PT",
            "NO",
            "NL",
            "HU",
            "IT",
            "GR"
        ],
        "masterID": "adGem12119",
        "devices": [
            "ipad",
            "iphone"
        ],
        "link": "https://api.adgem.com/v1/click?all=1&appid=4746&cid=12119&playerid={playerid}",
        "reward": "nothing",
        "source": "adgem",
        "isPromoted": true,
        "priority": 1,
        "isDisabled": false,
        "forceType": "null"
    }
}

这是数据结构:

const res = {
            appID: appID,
            cvr: cvr ,
            name: data[1].Offer.name,
            anchor: data[1].Offer.name,
            short_desc: data[1].Offer.short_description,
            long_desc: data[1].Offer.description,
            revenue: data[1].Offer.payout_usd,
            image: data[1].Offer.icon,
            geo: Object.keys(data[1].Country.include),
            masterID: "adGem" + data[1].Offer.campaign_id,
            devices: [...new Set(data[1].Device.include)],
            link: data[1].Offer.tracking_url,
            reward: "nothing",
            source: "adgem",
            isPromoted: true,
            priority: 1,
            isDisabled: false,
            forceType: "null",
          };

这是我正在使用的嵌套循环:

for (let i = 0; i < offersFast.length; i++) {
    
        for (let j = 0; j < offersFast[i].geo.length; j++) {
            for (let k = 0; k < offersFast[i].devices.length; k++) {
                
                const arrayName = offersFast[i].geo[j] + '_' + offersFast[i].devices[k]
                
                tempArray[arrayName] = offersFast[i] // this is defined, just not added in this question.
                
            }
        }
         
        
      }

【问题讨论】:

  • { "DE_ipad": { "appID": "1490262670", "cvr": "51.06666666666667", ... }, "DE_iphone": { "appID": "1490262670", "cvr" ": "51.06666666666667", ... } ... } 是你要制作的格式吗?
  • 是的。 {''DE_ipad'':[{appid, cvr}, {appid,cvr}], "DE_iphone": [{appid, cvr}, {appid, cvr}]
  • 好的。还是没搞清楚?

标签: javascript node.js arrays object


【解决方案1】:

请尝试以下代码。

let tempArray = {}
for (let i = 0; i < offersFast.length; i++) {
    
    for (let j = 0; j < offersFast[i].geo.length; j++) {
        for (let k = 0; k < offersFast[i].devices.length; k++) {
            
            const arrayName = offersFast[i].geo[j] + '_' + offersFast[i].devices[k]
            
            if (!Array.isArray(tempArray[arrayName]))
                tempArray[arrayName] = new Array()

            tempArray[arrayName].push(offersFast[i]) // this is defined, just not added in this question.
            
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-19
    • 2021-09-10
    • 2021-10-06
    • 2021-11-17
    • 2023-01-12
    • 2017-07-06
    • 1970-01-01
    • 2012-10-05
    相关资源
    最近更新 更多