Tengo 2 archivos json que importo con jquery y básicamente quiero unir las 2 matrices en 1 matriz. Así es como se ven los archivos json:

Players.json

{
    "players": [
        {
            "id": 109191123,
            "surname": "Farah",
            "full_name": "Robbie Farah",
            "short_name": "R. Farah",
            "other_names": "Robert",
            "jumper_number": 9,
            "position_code": "CEN1",
            "position_order": 9,
            "position_description": "Hooker",
            "is_captain": false,
            "is_interchange": false
        },
        {
            "id": 109509,
            "surname": "Rapana",
            "full_name": "Jordan Rapana",
            "short_name": "J. Rapana",
            "other_names": "Jordan",
            "jumper_number": 1,
            "position_code": "FBCK",
            "position_order": 1,
            "position_description": "Full Back",
            "is_captain": false,
            "is_interchange": false
        },
        {
            "id": 111285,
            "surname": "Waqa",
            "full_name": "Sisa Waqa",
            "short_name": "S. Waqa",
            "other_names": "Sisa",
            "jumper_number": 2,
            "position_code": "WING1",
            "position_order": 2,
            "position_description": "Wing",
            "is_captain": false,
            "is_interchange": false
        },
        {
            "id": 109861,
            "surname": "Croker",
            "full_name": "Jarrod Croker",
            "short_name": "J. Croker",
            "other_names": "Jarrod",
            "jumper_number": 3,
            "position_code": "CEN1",
            "position_order": 3,
            "position_description": "Centre",
            "is_captain": true,
            "is_interchange": false
        },
        {
            "id": 112814,
            "surname": "Lee",
            "full_name": "Edrick Lee",
            "short_name": "E. Lee",
            "other_names": "Edrick",
            "jumper_number": 5,
            "position_code": "CEN2",
            "position_order": 4,
            "position_description": "Centre",
            "is_captain": false,
            "is_interchange": false
        }
    ]
}

Stats.json

  {
    "player_stats": [
        {
            "id": 112814,
            "matches": "123",
            "tries": "11"
        },
        {
            "id": 111285,
            "matches": "234",
            "tries": "22"
        },
        {
            "id": 109861,
            "matches": "345",
            "tries": "33"
        },
        {
            "id": 109509,
            "matches": "456",
            "tries": "44"
        },
        {
            "id": 109510,
            "matches": "567",
            "tries": "55"
        }
    ]
}

Actualmente estoy buscando unir los datos en una nueva matriz para que uno de los elementos se vea así:

{
   player:'Lee',
   matches:123,
   tried:11
}

Básicamente 'unir' dos datos de los archivos json.

Actualmente hay una importación para los archivos json 2 y luego recorre ambas matrices tratando de encontrar una coincidencia en la propiedad id. ¿Hay una forma más eficiente de hacer esto preferiblemente usando vanilla js / jquery?

var players, stats, items = [];
    $.getJSON("data/players.json", function (data) {
        $.each(data, function (key, players) {
            $.getJSON("data/stats.json", function (data) {
                $.each(data, function (key, stats) {
                    //check stats available for players:
                    for (var i = 0; i < players.length; i++) {
                        for (var j = 0; j < stats.length; j++) {
                            var exists = items.some(function (item) {
                                return (item.player === players[i].full_name)
                            })
                            if (players[i].id === stats[j].id && !exists) {
                                items.push({
                                    player: players[i].full_name,
                                    matches: stats[j].matches,
                                    tries: stats[j].tries,
                                })
                            }
                        }
                    }

                });

            });
        });

    });
1
bier hier 14 nov. 2017 a las 11:12

2 respuestas

La mejor respuesta

Puede utilizar una combinación de .map y .find

var players = {
  "players": [{
      "id": 109191123,
      "surname": "Farah",
      "full_name": "Robbie Farah",
      "short_name": "R. Farah",
      "other_names": "Robert",
      "jumper_number": 9,
      "position_code": "CEN1",
      "position_order": 9,
      "position_description": "Hooker",
      "is_captain": false,
      "is_interchange": false
    },
    {
      "id": 109509,
      "surname": "Rapana",
      "full_name": "Jordan Rapana",
      "short_name": "J. Rapana",
      "other_names": "Jordan",
      "jumper_number": 1,
      "position_code": "FBCK",
      "position_order": 1,
      "position_description": "Full Back",
      "is_captain": false,
      "is_interchange": false
    },
    {
      "id": 111285,
      "surname": "Waqa",
      "full_name": "Sisa Waqa",
      "short_name": "S. Waqa",
      "other_names": "Sisa",
      "jumper_number": 2,
      "position_code": "WING1",
      "position_order": 2,
      "position_description": "Wing",
      "is_captain": false,
      "is_interchange": false
    },
    {
      "id": 109861,
      "surname": "Croker",
      "full_name": "Jarrod Croker",
      "short_name": "J. Croker",
      "other_names": "Jarrod",
      "jumper_number": 3,
      "position_code": "CEN1",
      "position_order": 3,
      "position_description": "Centre",
      "is_captain": true,
      "is_interchange": false
    },
    {
      "id": 112814,
      "surname": "Lee",
      "full_name": "Edrick Lee",
      "short_name": "E. Lee",
      "other_names": "Edrick",
      "jumper_number": 5,
      "position_code": "CEN2",
      "position_order": 4,
      "position_description": "Centre",
      "is_captain": false,
      "is_interchange": false
    }
  ]
}

var stats = {
  "player_stats": [{
      "id": 112814,
      "matches": "123",
      "tries": "11"
    },
    {
      "id": 111285,
      "matches": "234",
      "tries": "22"
    },
    {
      "id": 109861,
      "matches": "345",
      "tries": "33"
    },
    {
      "id": 109509,
      "matches": "456",
      "tries": "44"
    },
    {
      "id": 109510,
      "matches": "567",
      "tries": "55"
    }
  ]
}


var result = stats.player_stats.map(s => {
  var match = players.players.find(p => p.id == s.id);
  var name = "";

  if (match) {
    name = match.full_name;
  }


  return {
    player: name,
    matches: s.matches,
    tries: s.tries
  }
});

console.log(result)
.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}

Si lo desea para que si no hay coincidencia no esté incluido, puede cambiar el .map a .reduce

var players = {
  "players": [{
      "id": 109191123,
      "surname": "Farah",
      "full_name": "Robbie Farah",
      "short_name": "R. Farah",
      "other_names": "Robert",
      "jumper_number": 9,
      "position_code": "CEN1",
      "position_order": 9,
      "position_description": "Hooker",
      "is_captain": false,
      "is_interchange": false
    },
    {
      "id": 109509,
      "surname": "Rapana",
      "full_name": "Jordan Rapana",
      "short_name": "J. Rapana",
      "other_names": "Jordan",
      "jumper_number": 1,
      "position_code": "FBCK",
      "position_order": 1,
      "position_description": "Full Back",
      "is_captain": false,
      "is_interchange": false
    },
    {
      "id": 111285,
      "surname": "Waqa",
      "full_name": "Sisa Waqa",
      "short_name": "S. Waqa",
      "other_names": "Sisa",
      "jumper_number": 2,
      "position_code": "WING1",
      "position_order": 2,
      "position_description": "Wing",
      "is_captain": false,
      "is_interchange": false
    },
    {
      "id": 109861,
      "surname": "Croker",
      "full_name": "Jarrod Croker",
      "short_name": "J. Croker",
      "other_names": "Jarrod",
      "jumper_number": 3,
      "position_code": "CEN1",
      "position_order": 3,
      "position_description": "Centre",
      "is_captain": true,
      "is_interchange": false
    },
    {
      "id": 112814,
      "surname": "Lee",
      "full_name": "Edrick Lee",
      "short_name": "E. Lee",
      "other_names": "Edrick",
      "jumper_number": 5,
      "position_code": "CEN2",
      "position_order": 4,
      "position_description": "Centre",
      "is_captain": false,
      "is_interchange": false
    }
  ]
}

var stats = {
  "player_stats": [{
      "id": 112814,
      "matches": "123",
      "tries": "11"
    },
    {
      "id": 111285,
      "matches": "234",
      "tries": "22"
    },
    {
      "id": 109861,
      "matches": "345",
      "tries": "33"
    },
    {
      "id": 109509,
      "matches": "456",
      "tries": "44"
    },
    {
      "id": 109510,
      "matches": "567",
      "tries": "55"
    }
  ]
}


var result = stats.player_stats.reduce((a, s) => {
  var match = players.players.find(p => p.id == s.id);

  if (match) {
    a.push({
      player: match.full_name,
      matches: s.matches,
      tries: s.tries
    })
  }

  return a;
}, []);

console.log(result)
.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}
3
George 14 nov. 2017 a las 09:45

Puede utilizar array.prototype.map y array.prototype.find :

var players = {
    "players": [
        {
            "id": 109191123,
            "surname": "Farah",
            "full_name": "Robbie Farah",
            "short_name": "R. Farah",
            "other_names": "Robert",
            "jumper_number": 9,
            "position_code": "CEN1",
            "position_order": 9,
            "position_description": "Hooker",
            "is_captain": false,
            "is_interchange": false
        },
        {
            "id": 109509,
            "surname": "Rapana",
            "full_name": "Jordan Rapana",
            "short_name": "J. Rapana",
            "other_names": "Jordan",
            "jumper_number": 1,
            "position_code": "FBCK",
            "position_order": 1,
            "position_description": "Full Back",
            "is_captain": false,
            "is_interchange": false
        },
        {
            "id": 111285,
            "surname": "Waqa",
            "full_name": "Sisa Waqa",
            "short_name": "S. Waqa",
            "other_names": "Sisa",
            "jumper_number": 2,
            "position_code": "WING1",
            "position_order": 2,
            "position_description": "Wing",
            "is_captain": false,
            "is_interchange": false
        },
        {
            "id": 109861,
            "surname": "Croker",
            "full_name": "Jarrod Croker",
            "short_name": "J. Croker",
            "other_names": "Jarrod",
            "jumper_number": 3,
            "position_code": "CEN1",
            "position_order": 3,
            "position_description": "Centre",
            "is_captain": true,
            "is_interchange": false
        },
        {
            "id": 112814,
            "surname": "Lee",
            "full_name": "Edrick Lee",
            "short_name": "E. Lee",
            "other_names": "Edrick",
            "jumper_number": 5,
            "position_code": "CEN2",
            "position_order": 4,
            "position_description": "Centre",
            "is_captain": false,
            "is_interchange": false
        }
    ]
};

var stats =   {
    "player_stats": [
        {
            "id": 112814,
            "matches": "123",
            "tries": "11"
        },
        {
            "id": 111285,
            "matches": "234",
            "tries": "22"
        },
        {
            "id": 109861,
            "matches": "345",
            "tries": "33"
        },
        {
            "id": 109509,
            "matches": "456",
            "tries": "44"
        },
        {
            "id": 109510,
            "matches": "567",
            "tries": "55"
        }
    ]
};

var joined = stats.player_stats.map(stat => {
  var player = players.players.find(p => p.id === stat.id);
  return { player:  player ? player.surname : "", matches: stat.matches, tried: stat.tries };
});

console.log(joined);
0
Faly 14 nov. 2017 a las 08:27