过滤JSON对象

时间:2018-06-28 14:31:16

标签: arrays json node.js express object

我想问一问我如何才能达到我想要的结果,但我无法做到,这是json

[
 [
    {
        "id": "2059623",
        "name": "England-Belgium",
        "tournament_stageFK": "841049",
        "startdate": "2018/06/28 18:00:00",
        "status_type": "Not started",
        "status_descFK": "1",
        "n": "4",
        "ut": "2018/06/27 12:31:39",
        "tournamentFK": "9731",
        "tournament_templateFK": "77",
        "sportFK": "1",
        "tournament_stage_name": "World Cup Grp. G",
        "tournament_name": "2018",
        "tournament_template_name": "World Cup",
        "sport_name": "Football",
    },
    {
        "id": "2059624",
        "name": "Panama-Tunisia",
        "tournament_stageFK": "841049",
        "startdate": "2018/06/28 18:00:00",
        "status_type": "Not started",
        "status_descFK": "1",
        "n": "7",
        "ut": "2018/06/27 12:34:01",
        "tournamentFK": "9731",
        "tournament_templateFK": "77",
        "sportFK": "1",
        "tournament_stage_name": "World Cup Grp. G",
        "tournament_name": "2018",
        "tournament_template_name": "World Cup",
        "sport_name": "Football",
    },
],
[
    {
        "id": "2681772",
        "name": "Piteaa IF-Vittsjoe GIK",
        "tournament_stageFK": "853166",
        "startdate": "2018/06/28 17:00:00",
        "status_type": "Not started",
        "status_descFK": "1",
        "n": "1",
        "ut": "2017/12/18 15:02:33",
        "tournamentFK": "12327",
        "tournament_templateFK": "9089",
        "sportFK": "1",
        "tournament_stage_name": "Damallsvenskan",
        "tournament_name": "2018",
        "tournament_template_name": "Damallsvenskan",
        "sport_name": "Football",
    }

],
[
    {
        "id": "2705231",
        "name": "Ifoe Bromoella IF-IFK Haessleholm",
        "tournament_stageFK": "853950",
        "startdate": "2018/06/28 17:00:00",
        "status_type": "Not started",
        "status_descFK": "1",
        "n": "1",
        "ut": "2018/02/12 13:02:19",
        "tournamentFK": "12458",
        "tournament_templateFK": "170",
        "sportFK": "1",
        "tournament_stage_name": "2. Division Ostra Gotaland",
        "tournament_name": "2018",
        "tournament_template_name": "2. Division",
        "sport_name": "Football",
    }
  ]
 ]

如果您仔细观察,您会注意到游戏是按TournamentFK分组的,这很好,现在我要清除的是JSON这个我想要的结果是这样的:

{ 
"competitionId": "456789",
"competitionName": "World Cup 2018",
"matches":[
    {
        "matchId":1234,
       "teamA":"England",
       "teamB":"Poland",
       "time":"05:00",
        "result":"3-3"
        "stadium":"somewhere",
        "halfTimeScore":"1-2"
    },
    {
        "matchId":9876,
       "teamA":"Spain",
       "teamB":"Morocco",
       "time":"05:00",
        "result":"3-3"
       "stadium":"somewhere",
        "halfTimeScore":"2-1"
    }
]
}

我只需要获得一次TournamentFK和Tournament_template_name,然后循环进入每个锦标赛并做出一个明确的对象。

我的代码:

fixturesTofilter.forEach((fixture) => {
  sameGroup.push(fixture.tournamentFK);
  competitionName.push(fixture.tournament_template_name);
});
const groupByTournament = sameGroup.filter((item, pos) => sameGroup.indexOf(item) === pos);
const clearSameCompetition = competitionName.filter((item, pos) => competitionName.indexOf(item) === pos);
// console.log(clearSameCompetition);
groupByTournament.forEach((byTournamentFk, index) => {
  const grouped = groupBy(fixturesTofilter, item => item.tournamentFK);
  groupedFixtures.push(grouped.get(byTournamentFk));
});
console.log(groupedFixtures);

1 个答案:

答案 0 :(得分:0)

您可以通过lodash轻松展平该对象。由于JSON对象是扁平的,因此您只需添加tourFK和tour_template_name。希望这就是您想要的。

const _ = require('lodash');

const obj = [
 [
    {
        "id": "2059623",
        "name": "England-Belgium",
        "tournament_stageFK": "841049",
        "startdate": "2018/06/28 18:00:00",
        "status_type": "Not started",
        "status_descFK": "1",
        "n": "4",
        "ut": "2018/06/27 12:31:39",
        "tournamentFK": "9731",
        "tournament_templateFK": "77",
        "sportFK": "1",
        "tournament_stage_name": "World Cup Grp. G",
        "tournament_name": "2018",
        "tournament_template_name": "World Cup",
        "sport_name": "Football"
    },
    {
        "id": "2059624",
        "name": "Panama-Tunisia",
        "tournament_stageFK": "841049",
        "startdate": "2018/06/28 18:00:00",
        "status_type": "Not started",
        "status_descFK": "1",
        "n": "7",
        "ut": "2018/06/27 12:34:01",
        "tournamentFK": "9731",
        "tournament_templateFK": "77",
        "sportFK": "1",
        "tournament_stage_name": "World Cup Grp. G",
        "tournament_name": "2018",
        "tournament_template_name": "World Cup",
        "sport_name": "Football"
    }
],
[
    {
        "id": "2681772",
        "name": "Piteaa IF-Vittsjoe GIK",
        "tournament_stageFK": "853166",
        "startdate": "2018/06/28 17:00:00",
        "status_type": "Not started",
        "status_descFK": "1",
        "n": "1",
        "ut": "2017/12/18 15:02:33",
        "tournamentFK": "12327",
        "tournament_templateFK": "9089",
        "sportFK": "1",
        "tournament_stage_name": "Damallsvenskan",
        "tournament_name": "2018",
        "tournament_template_name": "Damallsvenskan",
        "sport_name": "Football"
    }

],
[
    {
        "id": "2705231",
        "name": "Ifoe Bromoella IF-IFK Haessleholm",
        "tournament_stageFK": "853950",
        "startdate": "2018/06/28 17:00:00",
        "status_type": "Not started",
        "status_descFK": "1",
        "n": "1",
        "ut": "2018/02/12 13:02:19",
        "tournamentFK": "12458",
        "tournament_templateFK": "170",
        "sportFK": "1",
        "tournament_stage_name": "2. Division Ostra Gotaland",
        "tournament_name": "2018",
        "tournament_template_name": "2. Division",
        "sport_name": "Football"
    }
  ]
 ]

const formatted = _.flattenDeep(obj)

const res = formatted.map(item => {
    const newObj = {
        matchId: item.tournamentFK,
        teamA: item.name.split("-")[0],
        teamB: item.name.split("-")[1],
        time: item.startdate,
        result:"how ??",
        stadium:"somewhere",
        halfTimeScore:"How?"
    }
    return newObj
})

console.log(res)