Jack Asked:2020-02-20 18:01:03 +0000 UTC2020-02-20 18:01:03 +0000 UTC 2020-02-20 18:01:03 +0000 UTC 如何处理多级数据? 772 例如,如果我的房子有房间有椅子,我该如何组织数据结构以便更方便地使用它们(删除添加、重新排序、更改)?最简单的选择是创建一组房屋对象,每个对象都有一组房间,每个房间都有一组椅子。但这非常不方便。 javascript 2 个回答 Voted Best Answer Kvilios 2020-02-20T18:34:52Z2020-02-20T18:34:52Z 但这非常不方便。 编写辅助函数,通过调用可以引用所需的房子、房间或椅子(以人类可读的形式): const houses = [{ // многоуровневые данные (дома, квартиры, стулья) name: '1/52 А', rooms: [{ name: '10', chairs: ['Стул 1', 'Стул 2'] }] }, { name: '6', rooms: [{ name: '10', chairs: ['Стул 1', 'Стул 2', 'Стул 3'] }, { name: '11', chairs: ['Стул 1', 'Стул 2', 'Стул 3', 'Стул 4'] }] }, { name: '3 корп. 4', rooms: [{ name: '55', chairs: ['Стул 1'] }] }]; console.log('Дом:', getHouseByName('1/52 А')); console.log('Комната:', getRoomByName('6', '11')); console.log('Стул:', getChairByName('3 корп. 4', '55', 'Стул 1')); console.log('Стулья:', getAllChairs('6', '10')); // функции-хелперы function getHouseByName(houseName) { // получаем нужный дом return houses.find((el) => el.name === houseName); } function getRoomByName(houseName, roomName) { // получаем нужную квартиру return getHouseByName(houseName).rooms.find((el) => el.name === roomName); } function getChairByName(houseName, roomName, chairName) { // получаем нужный стул return getAllChairs(houseName, roomName).find((el) => el === chairName); } function getAllChairs(houseName, roomName) { // получаем все стулья return getRoomByName(houseName, roomName).chairs; } Саске 2020-02-20T18:38:18Z2020-02-20T18:38:18Z <script> const ArrayHouse = [ { name: 'house1', rooms: [ { number: 3, chairs: [ { color: 'red', height: 10, width: 45 }, { color: 'red', height: 10, width: 45 }, { color: 'red', height: 10, width: 45 } ] }, { number: 1, chairs: [ { color: 'green', height: 10, width: 45 }, { color: 'black', height: 10, width: 45 }, { color: 'violet', height: 10, width: 45 } ] } ] }, { name: 'house2', rooms: [ { number: 3, chairs: [ { color: 'red', height: 10, width: 45 }, { color: 'red', height: 10, width: 45 }, { color: 'red', height: 10, width: 45 } ] }, { number: 1, chairs: [ { color: 'green', height: 10, width: 45 }, { color: 'black', height: 10, width: 45 }, { color: 'violet', height: 10, width: 45 } ] }, { number: 15, chairs: [ { color: 'green', height: 102, width: 45 }, { color: 'white', height: 110, width: 45 }, { color: 'violet', height: 150, width: 45 } ] } ] } ] class HouseContext { constructor() { console.log(this.getHouseByName('house1')); this.addHouse({ name: 'addHouse', rooms: [] }); console.log(ArrayHouse); console.log(this.getRoomBuNumber('house2', 1)); this.addRoom('addHouse', { number: 228, chairs:[] }); console.log(ArrayHouse); } getHouseByName(name) { return ArrayHouse.find(v => v.name == name); } addHouse(house) { ArrayHouse.push(house); } getRoomBuNumber(houseName, roomNumber) { return ArrayHouse.find(v => v.name == houseName).rooms.find(v => v.number == roomNumber); } addRoom(houseName, room) { ArrayHouse.find(v => v.name == houseName).rooms.push(room); } } new HouseContext(); </script>
编写辅助函数,通过调用可以引用所需的房子、房间或椅子(以人类可读的形式):