如何有效地合并2个json文件的数据? [英] How can I efficiently join data of 2 json files?

查看:95
本文介绍了如何有效地合并2个json文件的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个json文件,这些文件是通过jquery导入的,并且基本上想将2个数组合并为1个数组.这是json文件的样子:

I have 2 json files that I import with jquery and basically want to join the 2 arrays into 1 array. This is what the json files look like:

players.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

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"
        }
    ]
}

我目前正在寻求将数据合并到一个新数组中,因此其中一项看起来像:

I am currently looking to join the data into a new array so one of the items would look like:

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

基本上从json文件中联接"两个数据.

Basically 'joining' two data from the json files.

当前有2个json文件的导入,然后遍历两个数组以尝试在id属性上找到匹配项. 有没有更有效的方法,最好是使用香草js/jquery?

Currently there is an import for the 2 json files and then looping over both arrays trying to find a match on the id-property. Is there a more efficient way of doing this preferably using 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,
                                })
                            }
                        }
                    }

                });

            });
        });

    });

推荐答案

您可以使用

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;
}

如果需要它,以便在不匹配的情况下不包括在内,则可以将.map更改为

If you want it so that if there is no match it's not included you can change the .map to .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;
}

这篇关于如何有效地合并2个json文件的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆