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


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.push(...arr.slice(0));
    item.holes.length = length
    console.log(item.holes);
});
Ответить с цитированием