0

I have this:

'{
"Games": [
    {   "champion": 126,
        "season": 9
    },
    {
        "champion": 126,
        "season": 9
    }, {
        "champion": 126,
        "season": 8
    }`

And i want only to take champion number that's only from season 9. How can i do that?

Ais tis
  • 11
  • 5

3 Answers3

1

You can use Array.find:

const champion = data.Games.find(({season}) => season === 9)

or ES5

var champion = data.Games.find(function(champion) {
   return champion.season === 9;
});
Rob M.
  • 35,491
  • 6
  • 51
  • 50
1

I believe you must iterate over the array with conditional logic to capture and return any values that you are looking for.

Something like:

for (var i = 0; i < Games.length; i++) {
    if (Games[i].season == 9) {
        return(Games[i].champion);
    }
}
Casper-Evil
  • 31
  • 1
  • 2
0

Use Array.filter()

var season9 = Games.filter(function(elem) {
    return (elem.season === 9);
});

or in Es6

let season9 = Games.filter(elem => elem.season === 9);

then

var champions = season9.map(function(elem) {
   return elem.champion;
})

or in Es6

let champions = season9.map(elem => elem.champion);

console.log(champions) // => [126, 126]