如何有效地连接2个json文件的数据?

时间:2017-11-14 08:12:14

标签: javascript jquery arrays json

我有2个json文件,我用jquery导入,基本上想要将2个数组加入1个数组。这就是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"
        }
    ]
}

我目前正在寻找将数据加入到一个新数组中,因此其中一个项目如下:

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

基本上'加入'来自json文件的两个数据。

目前有2个json文件的导入,然后在两个数组上循环,试图在id-property上找到匹配项。 是否有一种更有效的方法,最好使用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,
                                })
                            }
                        }
                    }

                });

            });
        });

    });

2 个答案:

答案 0 :(得分:3)

您可以使用.map.find

的组合

&#13;
&#13;
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)
&#13;
.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}
&#13;
&#13;
&#13;

如果您需要,如果没有匹配,则可以将.map更改为.reduce

&#13;
&#13;
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)
&#13;
.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以使用 array.prototype.map 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);