2
let promiseArr = [];
    for (let i = 0; i < ISOarr.length; i++) {
        options.body.start_time = ISOarr[i];
        let newOptions = cloneOptions(options);
        promiseArr.push(newOptions);
    }
    console.log(promiseArr);

Returns an array of the same object. Clone method:

cloneOptions = options => {
    let clone = {};
    for( let key in options ) {
        clone[key] = options[key]
    }
    return clone;
}

So my question is how do I push an object that is not the same reference as the previous objects pushed, because even with it supposedly creating a new clone each loop it still somehow creates the same referenced object. In the loop if I console.log I get the proper output with the value of the key changed, but once it's pushed into the array and we console.log the array, all objects are the same. Any suggestions would be super helpful. Thank you in advance!

Casey Shore
  • 31
  • 1
  • 5

1 Answers1

1

Can you try this

cloneOptions = options => {
return JSON.parse(JSON.stringify(options))
}
Sourabh Somani
  • 2,138
  • 1
  • 13
  • 27