Показать сообщение отдельно
  #2 (permalink)  
Старый 17.01.2019, 19:11
Аватар для рони
Профессор
Отправить личное сообщение для рони Посмотреть профиль Найти все сообщения от рони
 
Регистрация: 27.05.2010
Сообщений: 33,064

s24344,
const data = [
  {
    locations: [
      {
        locationId: 0,
        players: [
          {
            holes: [
              {
                holeId: 1,
                score: 5
              },
              {
                holeId: 2,
                score: 5
              },
              {
                holeId: 3,
                score: 5
              },
              {
                holeId: 4,
                score: 5
              }
            ],
            firstName: "firstName 1",
            lastName: "lastName 1"
          },
          {
            holes: [
              {
                holeId: 1,
                score: 5
              },
              {
                holeId: 2,
                score: 5
              }
            ],
            firstName: "firstName 2",
            lastName: "lastName 2"
          },
          {
            holes: [
              {
                holeId: 1,
                score: 5
              }
            ],
            firstName: "firstName 3",
            lastName: "lastName 3"
          }
        ]
      }
    ]
  }
]

const players = data[0].locations[0].players;
const length = players.reduce((max, item) => {
const {length} = item.holes;
return  Math.max(max,length);
},0);
const arr = Array.from(new Array(length), () => ({score: '-'}));

players.forEach((item) => {
  item.holes = Object.assign([], arr, item.holes)
  console.log(item.holes);
});
Ответить с цитированием