skip to Main Content
{
"4": {
    "struttura": "Museo Archeologico",
    "casse": [{
        "id": "5",
        "nome": "Cassa a Terra DEMO"
    }]
},
"5": {
    "struttura": "Museo Fabrice",
    "casse": [{
        "id": "6",
        "nome": "Cassa Fabrice"
    }]
}}

i have this object in javascript. is it possible to have "casse", based on the key (4, or 5)

2

Answers


  1. const data = {
    "4": {
        "struttura": "Museo Archeologico",
        "casse": [{
            "id": "5",
            "nome": "Cassa a Terra DEMO"
        }]
    },
    "5": {
        "struttura": "Museo Fabrice",
        "casse": [{
            "id": "6",
            "nome": "Cassa Fabrice"
        }]
    }}
    
    

    The following should do the trick as @Bravo described:

    console.log(data["4"].casse)
    
    Login or Signup to reply.
  2. let obj = {
     "4": {
        "struttura": "Museo Archeologico",
        "casse": [{
            "id": "5",
            "nome": "Cassa a Terra DEMO"
        }]
     },
     "5": {
        "struttura": "Museo Fabrice",
        "casse": [{
            "id": "6",
            "nome": "Cassa Fabrice"
        }]
     }}
     let i = 4;
     console.log(obj[i].casse);
    

    This will get you there even when the key is a number

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search