0

So i have this 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
        }
      }

This is a json object of some items into some character's inventory. The key is basically the item slot ID in that inventory. What i want is to fill with empty arrays based on missing keys in AngularJS. For example if you take the first 2 items, first has 0 key and the second one has key 4 so there are missing 1,2,3 and i want to fill with empty arrays with those missing keys. And i want this process to repeat till the maximum of 255 arrays! Can anyone nicely please help me with a code or something?

2 Answers 2

1

you can do something like this:

for( var i=0;i<255;i++){
    if(inventory.data[i] == undefined){
        inventory.data[i] = {};
    }
}

I assume that you want to assign empty object to non existing keys.

Sign up to request clarification or add additional context in comments.

Comments

0

One other way of doing this would be like ;

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.