将json节点的一个对象与另一个同名的对象组合在一起

时间:2018-02-13 11:42:08

标签: javascript arrays angularjs json object

我有两个json文件,我希望共享一个json节点的一个对象,另一个json节点具有相同的节点名。

例如: -

Json file1: -

{
"teams": [
    {
        "id": 1,
        "name": "Australia",
        "shortName": "AUS",
        "flag": "https://s3/Images/flags/australia.png",
        "group": "d",
        "rank": "39",
        "starRating": "2",
    },
    {
        "id": 2,
        "name": "Argentina",
        "shortName": "AGR",
        "flag": "https://s3/Images/flags/ar.png",
        "group": "d",
        "rank": "5",
        "starRating": "5",
    }]
}

第二个json文件:

{
"standings": {
     "C": [
            {
                "group": "C",
                "rank": 0,
                "team": "France",
                "teamId": 773,
                "playedGames": 0,
                "crestURI": "https://upload.wikimedia.org/wikipedia/en/c/c3/Flag_of_France.svg",
                "points": 0,
                "goals": 0,
                "goalsAgainst": 0,
                "goalDifference": 0
            },
            {
                "group": "C",
                "rank": 0,
                "name": "Australia",
                "teamId": 779,
                "playedGames": 0,
                "crestURI": "",
                "points": 0,
                "goals": 0,
                "goalsAgainst": 0,
                "goalDifference": 0,

        }]

}

所以基本上,我想在第二个json中匹配"name":"Australia",并在第一个json的"name":"Australia"中添加AUS的整个对象,然后对每个国家重复。

有任何建议我如何实现这一目标?

已编辑: - 预期输出。

 {
    "standings": {
         "C": [
                {
                    "group": "C",
                    "rank": 0,
                    "team": "France",
                    "teamId": 773,
                    "playedGames": 0,
                    "crestURI": "https://upload.wikimedia.org/wikipedia/en/c/c3/Flag_of_France.svg",
                    "points": 0,
                    "goals": 0,
                    "goalsAgainst": 0,
                    "goalDifference": 0
                },
                {
                    "group": "C",
                    "rank": 0,
                    "name": "Australia",
                    "teamId": 779,
                    "playedGames": 0,
                    "crestURI": "",
                    "points": 0,
                    "goals": 0,
                    "goalsAgainst": 0,
                    "goalDifference": 0,
                    "id": 1,


                    "name": "Australia",
                    "shortName": "AUS",
                    "flag": "https://s3/Images/flags/australia.png",
                    "group": "d",
                    "rank": "39",
                    "starRating": "2",
                }]
    }
    }

1 个答案:

答案 0 :(得分:2)

这个问题已经回答here了。因为成为初学者的许多问题之一就是你不知道要搜索什么:无论如何,如果你想要改变团队的名字以后匹配这个名字!

<script>
    const arr1 = [
        {   "id": 1,
            "name": "Australia",
            "shortName": "AUS",
            "flag": "https://s3/Images/flags/australia.png",
            "group": "d",
            "rank": "39",
            "starRating": "2"
        },
        {
            "id": 2,
            "name": "Argentina",
            "shortName": "AGR",
            "flag": "https://s3/Images/flags/ar.png",
            "group": "d",
            "rank": "5",
            "starRating": "5"
        }];

     const arr2 = [
                {
                    "group": "C",
                    "rank": 0,
                    "team": "France",
                    "teamId": 773,
                    "playedGames": 0,
                    "crestURI": "https://upload.wikimedia.org/wikipedia/en/c/c3/Flag_of_France.svg",
                    "points": 0,
                    "goals": 0,
                    "goalsAgainst": 0,
                    "goalDifference": 0
                },
                {
                    "group": "C",
                    "rank": 0,
                    "name": "Australia",
                    "teamId": 779,
                    "playedGames": 0,
                    "crestURI": "",
                    "points": 0,
                    "goals": 0,
                    "goalsAgainst": 0,
                    "goalDifference": 0

            }];

    const result = arr1.map(val => {
        return Object.assign({}, val, arr2.filter(v => v.team === val.name)[0]);
    });
        console.log(result);
    </script>