Фильтровать объект JSON - PullRequest
0 голосов
/ 28 июня 2018

Я хотел бы спросить, как я могу добиться результата, чего я хочу, я пытался по-разному, но я не мог сделать, это 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",
    }
  ]
 ]

Если вы посмотрите поближе, вы заметите, что игры сгруппированы по tourFK. Это хорошо, теперь я хочу немного очистить этот 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"
    }
]
}

мне нужно получить только один раз tourFK и tour_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 голосов
/ 28 июня 2018

Вы можете легко сплющить этот объект с помощью lodash. Так как объект JSON плоский, вы можете просто добавить tourFK и router_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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...