我想更改数组列表中所有元素的值
if (country.hostel) {
country.hostel.forEach(function (hostel, index) {
hostel.room.forEach(function (room, index) {
room = {code:{value: BOOKED}};
});
});
}
但是我在代码中有一个警告,好像房间值不会改变
The value assigned is never used
最佳答案
你可以做:
(country.hostel || []).forEach(hostel => hostel.room.forEach((room) => {
room.code.value = BOOKED
}));
const country = {
hostel: [
{
room: [
{ code: { value: 'AAAAA' } },
{ code: { value: 'AAAAA' } },
{ code: { value: 'AAAAA' } }
]
}
]
};
const BOOKED = 'BOOKED';
(country.hostel || []).forEach(hostel => hostel.room.forEach((room) => {
room.code.value = BOOKED
}));
console.log(country);