const participants = [
{id: 1, name: 'Anakin Skywalker'},
{id: 2, name: 'Padmé Amidala'},
{id: 3, name: 'Luke Skywalker'},
{id: 4, name: 'Princess Leia'},
]
const hotel = [
{
room: [
{id: '', name: ''}
]
},
{
room: [
{id: '', name: ''},
{id: '', name: ''}
]
},
{
room: [
{id: '', name: ''}
]
}
]
const result = hotel.map(h => {
return h.room.map((r) => {
r.id = participants[0].id
r.name = participants[0].name
participants.shift()
return r
})
})
console.log(result)
[[{"id":1,"name":"Anakin Skywalker"}],
[{"id":2,"name":"Padmé Amidala"},{"id":3,"name":"Luke Skywalker"}],
[{"id":4,"name":"Princess Leia"}]]
const target = { a: '', b: 'V'}; // скопируем сюда
const source = { a: 'AAA', c: 'CCC' }; // отсюда
Object.assign(target, source);
target // { a: "AAA", b: "V", c: "CCC" }
participants
, то можно так:hotel.forEach(({ room }) => room.forEach(slot => Object.assign(slot, participants.pop())));
participants
.let i = 0;
hotel.forEach(({ room }) =>
room.forEach((slot) => Object.assign(slot, participants[i++]))
);
const checkIntoHotel = (hotel, people) => {
let index = 0;
const checkedHotel = hotel.map(({ room }) => room.map(({ id, name }) => {
const {id: parId, name: parName } = people[index];
index += 1;
return { id: parId, name: parName };
}))
return checkedHotel;
};