Find the id inside the array [] in json

1

Good evening!

I'm having a problem finding brackets case by case.

I'm trying to get the data from "stats" . But the only reference you have is "accountId" or "summonerName" . To get a reference to "stats" you need to get the "participantId" id that is inside "participantIdentities"

But my problem is that I can not get the id "participantId" .

JSON Base - Example

{  
   "gameId":1189987226,
   "participantIdentities":[  
      {  
         "player":{  
            "summonerName":"Khal Droggo",
            "accountId":1595535
         },
         "participantId":1
      },
      {  
         "player":{  
            "summonerName":"Lefetos",
            "accountId":211703728
         },
         "participantId":2
      }
   ],
   "participants":[  
      {  
         "stats":{  
            "champLevel":16,
            "participantId":1
         },
         "participantId":1
      },
      {  
         "stats":{  
            "champLevel":15,
            "participantId":2
         },
         "participantId":2
      }
   ]
}

I'm using javascript language.

function callback(andress, fn){
    $.ajax({
        url: andress,
        type: 'GET',
        dataType: 'json',
        error: function(){},
        success: fn
    });
}

function stats(accountId){
    callback('https://api.myjson.com/bins/1ew27p', function(retorno) {
        var obj = retorno.participantIdentities;
        Object.keys(obj).forEach(function(prop) {
            if (obj[prop].accountId == accountId) {
                document.write(obj[prop].participantId);
            }
        });
    });
}

stats(211703728);
    
asked by anonymous 17.09.2017 / 04:23

2 answers

0

The logic you are using to fetch find the player works, only access to the accountId field is not correct, this obj[prop].accountId .

If you make a foreach into participantIdentities it means that each object of this foreach will be something like:

{  
     "player":{  
        "summonerName":"Khal Droggo",
        "accountId":1595535
     },
     "participantId":1
}

This means that to access accountId you will have to .player.accountId instead of .accountId directly as you have.

So just change the if you have to:

if (obj[prop].player.accountId == accountId) { //agora .player.accountId

If player may not exist then please confirm that it exists using the hasOwnProperty which would look like this:

if (obj[prop].hasOwnProperty("player") && obj[prop].player.accountId == accountId) {

Example:

function callback(andress, fn){
    $.ajax({
        url: andress,
        type: 'GET',
        dataType: 'json',
        error: function(){},
        success: fn
    });
}

function stats(accountId){
    callback('https://api.myjson.com/bins/1ew27p', function(retorno) {
        var obj = retorno.participantIdentities;
        Object.keys(obj).forEach(function(prop) {
            if (obj[prop].hasOwnProperty("player") && obj[prop].player.accountId == accountId) { //if alterado aqui
                console.log('Achou jogador ${obj[prop].player.summonerName} com participantId de ${obj[prop].participantId}');
                return;//se achou não ve mais nenhum
            }
        });
    });
}

stats(211703728);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
17.09.2017 / 13:26
0

Based on the answer to a question from SOen

The best solution is to use the grep method of jQuery .

var participante = $.grep( obj, function( n, i ) {
    return n.player.accountId===accountId;
});

To recover the value of participantId

participante[0].participantId

Getting your code.

function callback(andress, fn){
    $.ajax({
        url: andress,
        type: 'GET',
        dataType: 'json',
        error: function(){},
        success: fn
    });
}

function stats(accountId){
    callback('https://api.myjson.com/bins/1ew27p', function(retorno) {
      var obj = retorno.participantIdentities;
      var participante = $.grep( obj, function( n, i ) {
        return n.player.accountId===accountId;
      });
      console.log(participante[0].participantId);
    });
}

stats(211703728);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
17.09.2017 / 04:46