简体   繁体   中英

Copy one array values to the the others

I have below array of JSON. I want to set schedules for the dayOfWeek which are not present inside the schedules of the other rooms

const data = {
  rooms: [
    {
      roomId: 1,
      schedules: []
    },
    {
      roomId: 2,
      schedules: [
        { home1: "05:05", dayOfWeek: 1, away: "20:30" },
        { home1: "06:05", dayOfWeek: 5, away: "21:30" },
        { home1: "07:05", dayOfWeek: 7, away: "22:30" }
      ]
    },
    {
      roomId: 3,
      schedules: []
    }
  ]
}

I need to copy the same schedules to the other rooms as well.

expected output

const finalArray = [
  { home1: "05:05", dayOfWeek: 1, away: "20:30", roomId: 1 }, //schedules from room2
  { home1: "06:05", dayOfWeek: 5, away: "21:30", roomId: 1 }, //schedules from room2
  { home1: "07:05", dayOfWeek: 7, away: "22:30", roomId: 1 }, //schedules from room2 

  { home1: "05:05", dayOfWeek: 1, away: "20:30", roomId: 3 }, //schedules from room2
  { home1: "06:05", dayOfWeek: 5, away: "21:30", roomId: 3 }, //schedules from room2
  { home1: "07:05", dayOfWeek: 7, away: "22:30", roomId: 3 }, //schedules from room2 
]

I have tried but could not get it work!!! Please help!!!

You can first find the source of truth room and then use Array.reduce to extract/copy the schedules to the others:

 const data = { rooms: [ { roomId: 1, schedules: [] }, { roomId: 2, schedules: [ { home1: "05:05", dayOfWeek: 1, away: "20:30" }, { home1: "06:05", dayOfWeek: 5, away: "21:30" }, { home1: "07:05", dayOfWeek: 7, away: "22:30" } ] }, { roomId: 3, schedules: [] } ] } const theRoom = data.rooms.find(x => x.schedules.length) const result = data.rooms.reduce((r,{roomId, schedules}) => { if(roomId != theRoom.roomId) r.push(...theRoom.schedules.map(x => ({ roomId, ...x }))) return r }, []) console.log(result) 

Just go through the array.

EDITED: edited by comment

 const data = { rooms: [ { roomId: 1, schedules: [] }, { roomId: 2, schedules: [ { home1: "05:05", dayOfWeek: 1, away: "20:30" }, { home1: "06:05", dayOfWeek: 5, away: "21:30" }, { home1: "07:05", dayOfWeek: 7, away: "22:30" } ] }, { roomId: 3, schedules: [] } ] } var roomIdWithSchedules = null; var schedulesOfRoomWithRoomIdWithSchedules = []; for(var i = 0; i < data.rooms.length; i++) { if(data.rooms[i].schedules && data.rooms[i].schedules.length) { roomIdWithSchedules = data.rooms[i].roomId; schedulesOfRoomWithRoomIdWithSchedules = data.rooms[i].schedules.map(function(x) { return JSON.parse(JSON.stringify(x)); }); break; } } var finalArray = []; if(roomIdWithSchedules != null) { for(var i = 0; i < data.rooms.length; i++) { if(roomIdWithSchedules === data.rooms[i].roomId) { continue; } for(var j = 0; j < schedulesOfRoomWithRoomIdWithSchedules.length; j++) { var item = JSON.parse(JSON.stringify(schedulesOfRoomWithRoomIdWithSchedules[j])); item.roomId = data.rooms[i].roomId; finalArray.push(item) } } } console.log(finalArray); 

如果您只是要将其放入新数组中,请执行以下操作:

var schedular = data.rooms[1].schedules

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM