0

I am working on React. I've created a Class to create a training program. In function of the training frequency number n selected by the user I need to create 1,2..n training programs. This is the class I created:

class Training {
  km = 0;
  programNum = 1;
  success = false;
  constructor(frequency, years) {
    this.frequency = frequency;
    this.years = years;
  }
}

This is the function for the training program:

const createTraining = (frequency, years) => {
    const newTraining = new Training(frequency, years);
    var temp = [];
    for (var i = 0; i < frequency; i++) {
      temp.push(newTraining);
    }}

 temp.forEach((item, i) => {
      item.programNum = i + 1;
    });
  console.log(temp);


// For frequency=4, it creates an array of 4 trainings:
(4) [Training, Training, Training, Training]
0: Training {km: 0, programNum: 4, success: false, frequency: '4', years: '2'}
1: Training {km: 0, programNum: 4, success: false, frequency: '4', years: '2'}
2: Training {km: 0, programNum: 4, success: false, frequency: '4', years: '2'}
3: Training {km: 0, programNum: 4, success: false, frequency: '4', years: '2'}
length: 4

Inside this array I would like to assign 1,2,3,4 so I user forEach but instead it assigns 4,4,4,4 to the key programNum I would like it to be like this:

(4) [Training, Training, Training, Training]
0: Training {km: 0, programNum: 1, success: false, frequency: '4', years: '2'}
1: Training {km: 0, programNum: 2, success: false, frequency: '4', years: '2'}
2: Training {km: 0, programNum: 3, success: false, frequency: '4', years: '2'}
3: Training {km: 0, programNum: 4, success: false, frequency: '4', years: '2'}
length: 4

I can't figure out how to do that.

2
  • it could be as easy as array.forEach((training, i) => obj.programNum= i);? or maybe just set that property before pushing each object in the resulting array. Or maybe even having it as an optional argument in the constructor. And last one :) you may model your own list of Training type. So that it owns the logic to flip the programNum when you add trainings to the list. Commented Jan 5, 2023 at 11:32
  • I have edited my question. I used temp.forEach((item, i) => {item.programNum = i + 1 })but it does not give 1,2,3,4 it assigned the length instead... Commented Jan 5, 2023 at 11:36

2 Answers 2

1

The problem is that your pushing a reference to the same object 4 times. Do this instead:

const createTraining = (frequency, years) => {

    for (var i = 0; i < frequency; i++) {
        const newTraining = new Training(frequency, years);
        newTraining.programNum = i + 1;
        temp.push(newTraining);

    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

A small modification to the executable statement should give you the desired functionality.

// your code
class Training {
  km = 0;
  programNum = 1;
  success = false;
  constructor(frequency, years) {
    this.frequency = frequency;
    this.years = years;
  }
}

// slightly modified code
const createTraining = (frequency, years) => {
  let temp = [];

  for (let i = 0; i < frequency; i++) {
    const newTraining = new Training(frequency, years);
    newTraining.programNum += i;
    temp.push(newTraining);
  }

  return temp;
};

// test result
const result = createTraining(4, 2);
console.log(result);

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.