casillas
casillas

Reputation: 16813

Retrieve object with a key in javascript

I have following object, and if I want to retrieve only soccer, then I put soccer as follows, sports['soccer'] does not bring it.

I wonder what I am missing here?

sports = [] ; 

sports = [{
    "soccer": {
        "type": "foot",
        "player": 11
    },
    "basketball": {
        "type": "hand",
        "player": 5
    }
}]

Upvotes: 0

Views: 87

Answers (4)

Barmar
Barmar

Reputation: 782653

As others have pointed out, you have an array of objects, not a single object. You can use the find() method to find the element that has the soccer property, and then access that property.

var soccer = sports.find(s => 'soccer' in s)['soccer'];

Upvotes: 0

Willem van der Veen
Willem van der Veen

Reputation: 36660

I think you really need to reiterate the basics of javascript a bit.

In JS we can create data structures on the fly with literal syntax. For example:

let normalArr = new Array();
let literalArr = [];

let normalObj = new Object();
let literalObj = {};

When you create arrays and objects with literal syntax you can initialize arrays with elements and object with properties on the fly. This is what exactly happened in your example:

sports = [{
    "soccer": {
        "type": "foot",
        "player": 11
    },
    "basketball": {
        "type": "hand",
        "player": 5
    }
}];

The code can be broken down in the following way:

  1. You created an array which was stored in the sports variable using the array literal syntax (created an array on the fly).
  2. You created 1 element of the array with the object literal syntax (creating an object on the fly)
  3. This object (located inside the array) has 2 properties, soccer and basketball which are also object created with object literal syntax.

To access one of there objects you need to do the following:

const sports = [{
    "soccer": {
        "type": "foot",
        "player": 11
    },
    "basketball": {
        "type": "hand",
        "player": 5
    }
}];

// accessing the first element of the sports array
console.log(sports[0].soccer);
console.log(sports[0].basketball);

Upvotes: 0

holaymolay
holaymolay

Reputation: 580

var sports is an array with objects inside.

If you set it up like this:

sports = [] ; 

sports = {
   "soccer": {
       "type": "foot",
       "player": 11
   },
   "basketball": {
      "type": "hand",
      "player": 5
   }
}

then you will be able to call sports['soccer'] or even sports.soccer.

Alternately, if you need it to remain an array, then you'll need to do more work. Something like this should do the trick.

for(i=0; i < sports.length; i++) {
  if("soccer" in sports[i]){
    console.log(sports[i].soccer.type);
    console.log(sports[i].soccer.player);
  }
}

The console.logs represent whatever you want to do with the values

Upvotes: 0

Code-Apprentice
Code-Apprentice

Reputation: 83587

Your current code creates an array with a single object. One solution is to just create an object instead:

sports = {
    "soccer": {
        "type": "foot",
        "player": 11
    },
    "basketball": {
        "type": "hand",
        "player": 5
    }
}

Now you can use sports.soccer or sports['soccer'] to access the soccer data.

If you really want an array of objects, you first need to subscript the array to get the first object:

sports[0].soccer

or

sports[0]['soccer']

Upvotes: 3

Related Questions