У меня JSON в этом формате. Я не могу решить эту проблему. Я использую Object.values и object.keys с Array.prototype.map (), но все, что мой алгоритм не делал того, что я хочу.

[
{
"2018-01-01": [
  {
    "firstname": "mati",
    "lastname": "mati",
    "userid": null,
    "total_minutes": 0,
    "free_minutes": 480,
    "task_date": "2018-01-01"
  },
  {
    "firstname": "andrzej",
    "lastname": "owsianka",
    "userid": null,
    "total_minutes": 0,
    "free_minutes": 480,
    "task_date": "2018-01-01"
  },
  {
    "firstname": "asd",
    "lastname": "asd",
    "userid": null,
    "total_minutes": 0,
    "free_minutes": 480,
    "task_date": "2018-01-01"
  }
],
"2018-01-02": [
  {
    "firstname": "mati",
    "lastname": "mati",
    "userid": null,
    "total_minutes": "70",
    "task_date": "2018-01-02",
    "free_minutes": 410
  },
  {
    "firstname": "andrzej",
    "lastname": "owsianka",
    "userid": null,
    "total_minutes": "360",
    "task_date": "2018-01-02",
    "free_minutes": 120
  },
  {
    "firstname": "asd",
    "lastname": "asd",
    "userid": null,
    "total_minutes": "30",
    "task_date": "2018-01-02",
    "free_minutes": 450
  }
],
"2018-01-03": [
  {
    "firstname": "mati",
    "lastname": "mati",
    "userid": null,
    "total_minutes": "0",
    "task_date": "2018-01-03",
    "free_minutes": 480
  },
  {
    "firstname": "andrzej",
    "lastname": "owsianka",
    "userid": null,
    "total_minutes": 0,
    "free_minutes": 480,
    "task_date": "2018-01-03"
  },
  {
    "firstname": "asd",
    "lastname": "asd",
    "userid": null,
    "total_minutes": 0,
    "free_minutes": 480,
    "task_date": "2018-01-03"
  }
]
}
]

Я хочу получить JSON в этом формате или очень похожем на этот формат

[
{
"firstname": "mati",
"lastname": "mati",
"2018-01-01": "480",
"2018-01-02": 480,
"2018-01-03": 480
},
{
"firstname": "andrzej",
"lastname": "owsianka",
"2018-01-01": "480",
"2018-01-02": 480,
"2018-01-03": 480
},
{
"firstname": "asd",
"lastname": "asd",
"2018-01-01": "480",
"2018-01-02": 480,
"2018-01-03": 480
}
]

Как мне сопоставить данные, чтобы получить что-то подобное? Я борюсь с этим около 2 часов, но толку не придумал.

-3
Adrian Miesikowski 3 Янв 2018 в 13:22

2 ответа

Лучший ответ

Вам нужно использовать reduce и forEach и сопоставить даты на основе firstname и lastname

var output = Object.values( arr.reduce( function(a, c){
   Object.keys( c ).forEach( function( date ){
      var arr2 = c[ date ];
      arr2.forEach( function(item){
        var keyObj = {firstname:item.firstname, lastname:item.lastname};
        var key = JSON.stringify( keyObj );
        a[ key ] = a[ key ] || keyObj;
        a[ key ][date] = item.free_minutes;
      });
   });
   return a;
} ,{}));

< Сильного > Demo

var arr = [{
  "2018-01-01": [{
      "firstname": "mati",
      "lastname": "mati",
      "userid": null,
      "total_minutes": 0,
      "free_minutes": 480,
      "task_date": "2018-01-01"
    },
    {
      "firstname": "andrzej",
      "lastname": "owsianka",
      "userid": null,
      "total_minutes": 0,
      "free_minutes": 480,
      "task_date": "2018-01-01"
    },
    {
      "firstname": "asd",
      "lastname": "asd",
      "userid": null,
      "total_minutes": 0,
      "free_minutes": 480,
      "task_date": "2018-01-01"
    }
  ],
  "2018-01-02": [{
      "firstname": "mati",
      "lastname": "mati",
      "userid": null,
      "total_minutes": "70",
      "task_date": "2018-01-02",
      "free_minutes": 410
    },
    {
      "firstname": "andrzej",
      "lastname": "owsianka",
      "userid": null,
      "total_minutes": "360",
      "task_date": "2018-01-02",
      "free_minutes": 120
    },
    {
      "firstname": "asd",
      "lastname": "asd",
      "userid": null,
      "total_minutes": "30",
      "task_date": "2018-01-02",
      "free_minutes": 450
    }
  ],
  "2018-01-03": [{
      "firstname": "mati",
      "lastname": "mati",
      "userid": null,
      "total_minutes": "0",
      "task_date": "2018-01-03",
      "free_minutes": 480
    },
    {
      "firstname": "andrzej",
      "lastname": "owsianka",
      "userid": null,
      "total_minutes": 0,
      "free_minutes": 480,
      "task_date": "2018-01-03"
    },
    {
      "firstname": "asd",
      "lastname": "asd",
      "userid": null,
      "total_minutes": 0,
      "free_minutes": 480,
      "task_date": "2018-01-03"
    }
  ]
}];

var output = Object.values( arr.reduce( function(a, c){
   Object.keys( c ).forEach( function( date ){
      var arr2 = c[ date ];
      arr2.forEach( function(item){
        var keyObj = {firstname:item.firstname, lastname:item.lastname};
        var key = JSON.stringify( keyObj );
        a[ key ] = a[ key ] || keyObj;
        a[ key ][date] = item.free_minutes;
      });
   });
   return a;
} ,{}));

console.log( output );
0
gurvinder372 3 Янв 2018 в 10:43

Вы должны перебирать и накапливать все вложенные части в одном простом аккумуляторе результатов, это псевдокод:

var result=[];

for each dates in json
        for each date in dates
            for each entry in date
                result[]=entry;
0
Luca C. 3 Янв 2018 в 10:26