user15395278
user15395278

Reputation:

The function makes the correct conclusion when console.log stands above the function call

The function makes the correct conclusion when console.log is placed above the function call. I'm not kidding, I checked several times by removing and adding the console.log. Version node.js v16.4.2. The input is correct. I also tried to reproduce the problem in a different environment, where I got the correct output without console.log.

async function styleQg(seance, date, durationSeance, questId, standardPrice) {
    const dateNow = (new Date()).toISOString().split('T')[0]
    seance = seance
        .filter(e => durationSeance === "3600" ?
            e.time.split(':')[1] === "00" :
            e.time.split(":")[1].split("")[1] !== "5")
        .map(e => ({date: date, ...e, price: standardPrice}))
    if (date === dateNow) {
        console.log(await expiredSeance(seance))
        seance = await expiredSeance(seance)
    }

    return seance
}

async function expiredSeance(seance) {

    seance = seance.map(e => {
        if (e.time < new Date().toLocaleString("ru-RU", {timeZone: "Europe/Moscow"}).split(' ')[1]) {
            return {...e, ...e.is_free = false}
        } else {
            return {...e}
        }
    })
    return seance
}

Input data(styleQg):

[
  { date: '2021-07-31', time: '10:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '11:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '12:00', is_free: false, price: 5000 },
  { date: '2021-07-31', time: '13:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '14:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '15:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '16:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '17:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '18:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '19:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '20:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '21:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '22:00', is_free: true, price: 5000 }
]

Output data(with console.log):

[
  { date: '2021-07-31', time: '10:00', is_free: false, price: 5000 },
  { date: '2021-07-31', time: '11:00', is_free: false, price: 5000 },
  { date: '2021-07-31', time: '12:00', is_free: false, price: 5000 },
  { date: '2021-07-31', time: '13:00', is_free: false, price: 5000 },
  { date: '2021-07-31', time: '14:00', is_free: false, price: 5000 },
  { date: '2021-07-31', time: '15:00', is_free: false, price: 5000 },
  { date: '2021-07-31', time: '16:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '17:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '18:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '19:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '20:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '21:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '22:00', is_free: true, price: 5000 }
]

Output data(without console.log):

[
  { date: '2021-07-31', time: '10:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '11:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '12:00', is_free: false, price: 5000 },
  { date: '2021-07-31', time: '13:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '14:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '15:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '16:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '17:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '18:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '19:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '20:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '21:00', is_free: true, price: 5000 },
  { date: '2021-07-31', time: '22:00', is_free: true, price: 5000 }
]

Upvotes: 0

Views: 58

Answers (1)

VLAZ
VLAZ

Reputation: 29088

The problem is that you are doing mutation while spreading: {...e, ...e.is_free = false} This will not transfer the new value you assign to the new object you create. Instead it just changes the old object. Using ...e.is_free = false will try to spread the assignment value false into the object which does not do anything.

It is not the console.log that changes anything, it is calling the function twice. The second call will use the updated values:

const x = { is_free: true };

//changes `x.is_free` to `false` but `y` already used the old value
const y = { ...x, ...x.is_free = false}; 

//changes `x.is_free` is already `false` so `z` gets that value
const z = { ...x, ...x.is_free = false};

console.log(x);
console.log(y);
console.log(z);

The correct way to copy an object and change a value is {...e, is_free: false} that will not rely on any mutations and side-effects:

const x = { is_free: true };
const y = { ...x, is_free: false}; 
const z = { ...x, is_free: false};

console.log(x);
console.log(y);
console.log(z);

Upvotes: 1

Related Questions