0

Here is my code:

let purchasesInfoArray = []

console.log('buyersInfo', buyersInfo)

buyersInfo.forEach((runner) => {
  console.log('runner', runner)

  let purchaseInfo = allPurchase
  let purchaseFunction = {}

  counter = counter + 1;

  console.log('counter é:', counter)

  delete purchaseInfo.friends
  purchaseInfo.priceCents = runner.price
  purchaseInfo.totalTaxPaid = 600
  purchaseInfo.iuguTaxPaid = 198
  purchaseInfo.kmaisTaxPaid = 402
  purchaseInfo.status = 'paid'
  purchaseInfo.runnerCpf = runner.runnerCpf
  purchaseInfo.modality = runner.modality
  purchaseInfo.sex = runner.sex
  purchaseInfo.age = runner.age
  purchaseInfo.birth = runner.birth
  purchaseInfo.buyerName = runner.buyerName

  console.log('número de corredor é:', (oldCounter + counter))

  purchaseInfo.runnerNumber = (oldCounter + counter)
  purchasesInfoArray.push(purchaseInfo)

  console.log('purchase info:', purchaseInfo)
})

console.log(purchasesInfoArray)

Why my purchasesInfoArray is just populating with the last value??

My buyersInfo are:

[{ age: 25,
   birth: '15/07/1994',
   buyerName: 'Romário 2',
   kit: 'Básico',
   modality: 'Caminhada',
   price: 60,
   runnerCpf: '012.499.862-32',
   sex: 'man' },
 { age: 25,
   birth: '14/07/1994',
   buyerName: 'Romário 1',
   kit: 'Básico',
   modality: '5 KM',
   price: 60,
   runnerCpf: '012.499.862-30',
   sex: 'man' }]

My purchasesInfoArray is returning two objects with the last object from array.

4
  • 2
    What is allPurchase? Every index in the array is referencing that object. You could clone it for each index something like let purchaseInfo = { ...allPurchase } should work Commented Jul 30, 2019 at 18:59
  • allPurchase is a big object with many fields, i get it inside loop, then i change the fields that are different Commented Jul 30, 2019 at 19:01
  • 1
    @eeerrrttt you have to make a copy of that object. Commented Jul 30, 2019 at 19:04
  • it is on top of my node function. let allPurchase = snap.after.data() (which is an object with many fields from firebase) Commented Jul 30, 2019 at 19:04

1 Answer 1

1

I would change your code to

let purchasesInfoArray = []
console.log('buyersInfo', buyersInfo)
buyersInfo.forEach((runner) => {
    console.log('runner', runner)
    let purchaseInfo = {...allPurchase} // <-- This line
    let purchaseFunction = {}
    counter = counter + 1;
    console.log('counter é:', counter)
    delete purchaseInfo.friends
    purchaseInfo.priceCents = runner.price
    purchaseInfo.totalTaxPaid = 600
    purchaseInfo.iuguTaxPaid = 198
    purchaseInfo.kmaisTaxPaid = 402
    purchaseInfo.status = 'paid'
    purchaseInfo.runnerCpf = runner.runnerCpf
    purchaseInfo.modality = runner.modality
    purchaseInfo.sex = runner.sex
    purchaseInfo.age = runner.age
    purchaseInfo.birth = runner.birth
    purchaseInfo.buyerName = runner.buyerName
    console.log('número de corredor é:', (oldCounter + counter))
    purchaseInfo.runnerNumber = (oldCounter + counter)
    purchasesInfoArray.push(purchaseInfo)
    console.log('purchase info:', purchaseInfo)
})

To make sure every purchaseInfo is a new reference, otherwise you are just updating allPurchase with every assignment, thus updating every purchaseInfo as they are all sharing the same memory reference.

If you can't use the spread operator because of your javascript version, you can alternatively use the assign method

let purchaseInfo = Object.assign({}, allPurchase);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.