Copy one array values to the the others

copy one array to another c++
copy array to another array javascript
copy one array to another in c
copy one array to another java
c program to copy one array to another array using pointers
how to copy one array to another in python
assign one array to another array java
copy elements from one array to another python

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)

Array Copy in Java, Which allocates a new array, copies over the elements of a , and returns the new array. Or b = new int[a.length]; System.arraycopy(a, 0, b, 0,  I have 2 array of object. 1st array of object have property which I want to copy to other array. 1st array of object HotelRoomResponse[] hr=new HotelRoomResponse[100]; 2nd array of object RateI

if you want to simply put it into new array just do it:

var schedular = data.rooms[1].schedules

Best way to copy from one array to another, There are mainly four different ways to copy all elements of one array into another array in Java. 1. Manually 2. Arrays.copyOf() 3. System. Step by step descriptive logic to copy an array. Input size and elements in array, store it in some variable say size and source. Declare another array dest to store copy of source. Now, to copy all elements from source to dest array, you just need to iterate through each element of source.

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);

How to Copy One Array to Another in Java, C programming, exercises, solution: Write a program in C to copy the elements of one array into another array. The setOrders method loops through the orders array and assign current iteration item to cloneOrders array. So when the function returned, you'll have the last item of orders stored in cloneOrders not the complete array. – Ms.Tamil Nov 12 '18 at 9:55

C exercises: Copy the elements of one array into another array , Write a C program to input elements in array and copy all elements of first array into second array. How to copy array elements to another array  Logic to copy one array to another array using pointers Step by step descriptive logic to copy one array to another using pointers. Input size and elements in first array, store it in some variable say size and source_array. Declare another array say dest_array to store copy of source_array.

C program to copy all elements of one array to another, Array in java can be copied to another array using the following ways. Create a new array of the same length and copy each element. You might need to copy data from one array to another. For example, you can base a new array on the content of an existing array. It's also safer to make changes to a copy of an array rather than to change the original and potentially damage the data. Listing 9-4 shows an example of code that you can use to copy one array to another.

Array Copy in Java, In this program, we need to copy all the elements of one array into another. This can be accomplished by looping through the first array and store the elements of​  Copy the elements one array into another array : ----- Input the number of elements to be stored in the array :3 Input 3 elements in the array : element - 0 : 15 element - 1 : 10 element - 2 : 12 The elements stored in the first array are : 15 10 12 The elements copied into the second array are : 15 10 12

Comments
  • Should the final array be a new array or is it the rooms array? Or do you want to copy the schedules from roomId2 to the other rooms schedules array?
  • @JustAMicrobe Its a new array.
  • well what to do with the one which contains schedules ?
  • @CodeManiac Need to copy to the other rooms schedules. Contains roomId. Please have a look at the finalArray
  • Well you didn't added roomId 2 there you want it there or not in finalArray ?
  • What are the arguments inside the array.reduce(argument, argument)?
  • I am getting roomId: 2 I need others roomId i.e. 1 and 3