I have two arrays tasks
and updatedTasks
. When the first api call is made, one of the properties is updated. When I make a copy of tasks
array and make a second API to update other property, it adds sadditional properties to updatedTasks
array. The weird thing is I get the expected result, but both tasks
and updatedTasks
have same properties.
Code:
const _tasks = []
const _updatedTasks = []
const obj = {
name: 'task',
zone: 'zone'
}
const zone = 'abc'
const response = { taskList: [ obj ]};
const response2 = {time: '123'}
response.taskList.map((task)=>{
task.zone = zone
const obj = {...task}
_tasks.push(obj)
})
console.log(_tasks)
const copy = [..._tasks]
copy.map((task)=>{
const resp = response2
task.time = resp.time
_updatedTasks.push(task)
})
console.log(_updatedTasks)
How can I make sure, tasks
just updates zone
and updateTasks
updates both zone
and time
?
Fiddle Link: https://playcode.io/944175