我正在从一个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.
}
}
}
发布于 2021-07-06 22:24:44
请尝试使用以下代码。
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.
}
}
}
https://stackoverflow.com/questions/68268148
复制相似问题