所以我有这个json
"inventory": {
"data": {
"0": {
"id": 637,
"upgrade": 0,
"bind": 0,
"slot": 0,
"name": "Slot Extender (Low)",
"desc": null
},
"4": {
"id": 3247,
"upgrade": 0,
"bind": 0,
"slot": 4,
"name": "Lamp of Dazzlement (Lv. 4)",
"desc": null
},
"7": {
"id": 637,
"upgrade": 0,
"bind": 0,
"slot": 7,
"name": "Slot Extender (Low)",
"desc": null
},
"8": {
"id": 1454,
"upgrade": 0,
"bind": 0,
"slot": 8,
"name": "Vampiric Earring +7",
"desc": null
},
"11": {
"id": 12,
"upgrade": 0,
"bind": 0,
"slot": 11,
"name": "Return Stone",
"desc": null
},
"12": {
"id": 2425,
"upgrade": 0,
"bind": 0,
"slot": 12,
"name": "Vital Potion (Lv. 2)",
"desc": null
},
"14": {
"id": 3094,
"upgrade": 0,
"bind": 0,
"slot": 14,
"name": "Holy Water of Critical Strike (30 min)",
"desc": null
},
"16": {
"id": 3247,
"upgrade": 0,
"bind": 0,
"slot": 16,
"name": "Lamp of Dazzlement (Lv. 4)",
"desc": null
},
"20": {
"id": 10,
"upgrade": 0,
"bind": 0,
"slot": 20,
"name": "Upgrade Core (Medium)",
"desc": null
},
"22": {
"id": 10,
"upgrade": 0,
"bind": 0,
"slot": 22,
"name": "Upgrade Core (Medium)",
"desc": null
},
"23": {
"id": 2444,
"upgrade": 0,
"bind": 0,
"slot": 23,
"name": "Strike Potion (Lv. 1)",
"desc": null
},
"24": {
"id": 19,
"upgrade": 9,
"bind": 4,
"slot": 24,
"name": "Citrine Orb",
"desc": null
},
"26": {
"id": 124,
"upgrade": 15,
"bind": 1,
"slot": 26,
"name": "Aramid Battlesuit",
"desc": null
},
"28": {
"id": 2704,
"upgrade": 0,
"bind": 0,
"slot": 28,
"name": "Weapon Option Scroll (High)",
"desc": null
},
"29": {
"id": 214,
"upgrade": 15,
"bind": 4,
"slot": 29,
"name": "Aramid Battleboots",
"desc": null
},
"44": {
"id": 169,
"upgrade": 13,
"bind": 4,
"slot": 44,
"name": "Aramid Battlegloves",
"desc": null
},
"47": {
"id": 663,
"upgrade": 0,
"bind": 0,
"slot": 47,
"name": "Force Regen. Potion (Lv. 1)",
"desc": null
},
"48": {
"id": 1,
"upgrade": 0,
"bind": 0,
"slot": 48,
"name": "Upgrade Core (High)",
"desc": null
},
"56": {
"id": 2342,
"upgrade": 0,
"bind": 0,
"slot": 56,
"name": "Holy Water of Fighter",
"desc": null
},
"59": {
"id": 2381,
"upgrade": 0,
"bind": 0,
"slot": 59,
"name": "Enchant Safeguard (Highest)",
"desc": null
},
"64": {
"id": 2338,
"upgrade": 0,
"bind": 0,
"slot": 64,
"name": "Holy Water of Vitality",
"desc": null
},
"65": {
"id": 1116,
"upgrade": 0,
"bind": 0,
"slot": 65,
"name": "Snow Ice MP Potion",
"desc": null
},
"66": {
"id": 5,
"upgrade": 0,
"bind": 0,
"slot": 66,
"name": "HP Potion (Lv. 3)",
"desc": null
},
"68": {
"id": 1381,
"upgrade": 0,
"bind": 0,
"slot": 68,
"name": "Copper Coin",
"desc": null
},
"72": {
"id": 320,
"upgrade": 0,
"bind": 0,
"slot": 72,
"name": "Life Absorb Ring +2",
"desc": null
},
"74": {
"id": 582,
"upgrade": 0,
"bind": 0,
"slot": 74,
"name": "Force Core (Low)",
"desc": null
},
"75": {
"id": 2,
"upgrade": 0,
"bind": 0,
"slot": 75,
"name": "Force Core (High)",
"desc": null
},
"76": {
"id": 2339,
"upgrade": 0,
"bind": 0,
"slot": 76,
"name": "Holy Water of Speed",
"desc": null
},
"80": {
"id": 3278,
"upgrade": 0,
"bind": 0,
"slot": 80,
"name": "Skill Book (Split Specialty Stage 3)",
"desc": null
},
"82": {
"id": 3276,
"upgrade": 0,
"bind": 0,
"slot": 82,
"name": "Skill Book (Sword Splitter)",
"desc": null
},
"84": {
"id": 3277,
"upgrade": 0,
"bind": 0,
"slot": 84,
"name": "Skill Book (Split Specialty Stage 2)",
"desc": null
},
"86": {
"id": 34,
"upgrade": 5,
"bind": 1,
"slot": 86,
"name": "Citrine Crystal",
"desc": null
},
"96": {
"id": 3293,
"upgrade": 0,
"bind": 0,
"slot": 96,
"name": "Minesta Training Book Chapter 15",
"desc": null
},
"105": {
"id": 1214,
"upgrade": 0,
"bind": 0,
"slot": 105,
"name": "Upgrade Core (Highest)",
"desc": null
},
"106": {
"id": 1214,
"upgrade": 0,
"bind": 0,
"slot": 106,
"name": "Upgrade Core (Highest)",
"desc": null
},
"107": {
"id": 1214,
"upgrade": 0,
"bind": 0,
"slot": 107,
"name": "Upgrade Core (Highest)",
"desc": null
},
"128": {
"id": 1116,
"upgrade": 0,
"bind": 0,
"slot": 128,
"name": "Snow Ice MP Potion",
"desc": null
},
"129": {
"id": 2345,
"upgrade": 0,
"bind": 0,
"slot": 129,
"name": "Holy Water of Flawless Defense",
"desc": null
},
"130": {
"id": 1116,
"upgrade": 0,
"bind": 0,
"slot": 130,
"name": "Snow Ice MP Potion",
"desc": null
},
"131": {
"id": 1115,
"upgrade": 0,
"bind": 0,
"slot": 131,
"name": "Snow Star HP Potion",
"desc": null
},
"132": {
"id": 1115,
"upgrade": 0,
"bind": 0,
"slot": 132,
"name": "Snow Star HP Potion",
"desc": null
},
"133": {
"id": 1115,
"upgrade": 0,
"bind": 0,
"slot": 133,
"name": "Snow Star HP Potion",
"desc": null
},
"134": {
"id": 1116,
"upgrade": 0,
"bind": 0,
"slot": 134,
"name": "Snow Ice MP Potion",
"desc": null
},
"142": {
"id": 2,
"upgrade": 0,
"bind": 0,
"slot": 142,
"name": "Force Core (High)",
"desc": null
},
"196": {
"id": 1275,
"upgrade": 0,
"bind": 0,
"slot": 196,
"name": "Periodical Remote shop card",
"desc": null
}
}
这是某些角色的一些项目的json对象。关键基本上是该库存中的项目槽ID。 我想要的是基于AngularJS中缺少的键填充空数组。例如,如果你取前两个项目,第一个有0个密钥,第二个有密钥4,所以缺少1,2,3,我想填充缺少密钥的空数组。我希望这个过程重复到最多255个阵列! 任何人都可以帮我一个代码或什么?
答案 0 :(得分:1)
你可以这样做:
for( var i=0;i<255;i++){
if(inventory.data[i] == undefined){
inventory.data[i] = {};
}
}
我假设您要将空对象分配给不存在的键。
答案 1 :(得分:0)
另一种方法是这样做;
var data = {
"0": {
"id": 637,
"upgrade": 0,
"bind": 0,
"slot": 0,
"name": "Slot Extender (Low)",
"desc": null
},
"4": {
"id": 3247,
"upgrade": 0,
"bind": 0,
"slot": 4,
"name": "Lamp of Dazzlement (Lv. 4)",
"desc": null
},
"7": {
"id": 637,
"upgrade": 0,
"bind": 0,
"slot": 7,
"name": "Slot Extender (Low)",
"desc": null
},
"8": {
"id": 1454,
"upgrade": 0,
"bind": 0,
"slot": 8,
"name": "Vampiric Earring +7",
"desc": null
},
"11": {
"id": 12,
"upgrade": 0,
"bind": 0,
"slot": 11,
"name": "Return Stone",
"desc": null
},
"12": {
"id": 2425,
"upgrade": 0,
"bind": 0,
"slot": 12,
"name": "Vital Potion (Lv. 2)",
"desc": null
},
"14": {
"id": 3094,
"upgrade": 0,
"bind": 0,
"slot": 14,
"name": "Holy Water of Critical Strike (30 min)",
"desc": null
},
"16": {
"id": 3247,
"upgrade": 0,
"bind": 0,
"slot": 16,
"name": "Lamp of Dazzlement (Lv. 4)",
"desc": null
},
"20": {
"id": 10,
"upgrade": 0,
"bind": 0,
"slot": 20,
"name": "Upgrade Core (Medium)",
"desc": null
},
"22": {
"id": 10,
"upgrade": 0,
"bind": 0,
"slot": 22,
"name": "Upgrade Core (Medium)",
"desc": null
},
"23": {
"id": 2444,
"upgrade": 0,
"bind": 0,
"slot": 23,
"name": "Strike Potion (Lv. 1)",
"desc": null
},
"24": {
"id": 19,
"upgrade": 9,
"bind": 4,
"slot": 24,
"name": "Citrine Orb",
"desc": null
},
"26": {
"id": 124,
"upgrade": 15,
"bind": 1,
"slot": 26,
"name": "Aramid Battlesuit",
"desc": null
},
"28": {
"id": 2704,
"upgrade": 0,
"bind": 0,
"slot": 28,
"name": "Weapon Option Scroll (High)",
"desc": null
},
"29": {
"id": 214,
"upgrade": 15,
"bind": 4,
"slot": 29,
"name": "Aramid Battleboots",
"desc": null
},
"44": {
"id": 169,
"upgrade": 13,
"bind": 4,
"slot": 44,
"name": "Aramid Battlegloves",
"desc": null
},
"47": {
"id": 663,
"upgrade": 0,
"bind": 0,
"slot": 47,
"name": "Force Regen. Potion (Lv. 1)",
"desc": null
},
"48": {
"id": 1,
"upgrade": 0,
"bind": 0,
"slot": 48,
"name": "Upgrade Core (High)",
"desc": null
},
"56": {
"id": 2342,
"upgrade": 0,
"bind": 0,
"slot": 56,
"name": "Holy Water of Fighter",
"desc": null
},
"59": {
"id": 2381,
"upgrade": 0,
"bind": 0,
"slot": 59,
"name": "Enchant Safeguard (Highest)",
"desc": null
},
"64": {
"id": 2338,
"upgrade": 0,
"bind": 0,
"slot": 64,
"name": "Holy Water of Vitality",
"desc": null
},
"65": {
"id": 1116,
"upgrade": 0,
"bind": 0,
"slot": 65,
"name": "Snow Ice MP Potion",
"desc": null
},
"66": {
"id": 5,
"upgrade": 0,
"bind": 0,
"slot": 66,
"name": "HP Potion (Lv. 3)",
"desc": null
},
"68": {
"id": 1381,
"upgrade": 0,
"bind": 0,
"slot": 68,
"name": "Copper Coin",
"desc": null
},
"72": {
"id": 320,
"upgrade": 0,
"bind": 0,
"slot": 72,
"name": "Life Absorb Ring +2",
"desc": null
},
"74": {
"id": 582,
"upgrade": 0,
"bind": 0,
"slot": 74,
"name": "Force Core (Low)",
"desc": null
},
"75": {
"id": 2,
"upgrade": 0,
"bind": 0,
"slot": 75,
"name": "Force Core (High)",
"desc": null
},
"76": {
"id": 2339,
"upgrade": 0,
"bind": 0,
"slot": 76,
"name": "Holy Water of Speed",
"desc": null
},
"80": {
"id": 3278,
"upgrade": 0,
"bind": 0,
"slot": 80,
"name": "Skill Book (Split Specialty Stage 3)",
"desc": null
},
"82": {
"id": 3276,
"upgrade": 0,
"bind": 0,
"slot": 82,
"name": "Skill Book (Sword Splitter)",
"desc": null
},
"84": {
"id": 3277,
"upgrade": 0,
"bind": 0,
"slot": 84,
"name": "Skill Book (Split Specialty Stage 2)",
"desc": null
},
"86": {
"id": 34,
"upgrade": 5,
"bind": 1,
"slot": 86,
"name": "Citrine Crystal",
"desc": null
},
"96": {
"id": 3293,
"upgrade": 0,
"bind": 0,
"slot": 96,
"name": "Minesta Training Book Chapter 15",
"desc": null
},
"105": {
"id": 1214,
"upgrade": 0,
"bind": 0,
"slot": 105,
"name": "Upgrade Core (Highest)",
"desc": null
},
"106": {
"id": 1214,
"upgrade": 0,
"bind": 0,
"slot": 106,
"name": "Upgrade Core (Highest)",
"desc": null
},
"107": {
"id": 1214,
"upgrade": 0,
"bind": 0,
"slot": 107,
"name": "Upgrade Core (Highest)",
"desc": null
},
"128": {
"id": 1116,
"upgrade": 0,
"bind": 0,
"slot": 128,
"name": "Snow Ice MP Potion",
"desc": null
},
"129": {
"id": 2345,
"upgrade": 0,
"bind": 0,
"slot": 129,
"name": "Holy Water of Flawless Defense",
"desc": null
},
"130": {
"id": 1116,
"upgrade": 0,
"bind": 0,
"slot": 130,
"name": "Snow Ice MP Potion",
"desc": null
},
"131": {
"id": 1115,
"upgrade": 0,
"bind": 0,
"slot": 131,
"name": "Snow Star HP Potion",
"desc": null
},
"132": {
"id": 1115,
"upgrade": 0,
"bind": 0,
"slot": 132,
"name": "Snow Star HP Potion",
"desc": null
},
"133": {
"id": 1115,
"upgrade": 0,
"bind": 0,
"slot": 133,
"name": "Snow Star HP Potion",
"desc": null
},
"134": {
"id": 1116,
"upgrade": 0,
"bind": 0,
"slot": 134,
"name": "Snow Ice MP Potion",
"desc": null
},
"142": {
"id": 2,
"upgrade": 0,
"bind": 0,
"slot": 142,
"name": "Force Core (High)",
"desc": null
},
"196": {
"id": 1275,
"upgrade": 0,
"bind": 0,
"slot": 196,
"name": "Periodical Remote shop card",
"desc": null
}
},
ar = Array(255).fill().map((e,i) => data[i] || {}); // all in array
console.log(ar);
obj = Array(255).fill().reduce((p,c,i) => (p[i] = data[i] || {},p),{}); // all in object
console.log(obj);