1

I try to change the values of multiple objects in an array of objects.

// for..of loop with variable i to access second array to get values

    const AntraegeListe = new Array();
for (let i = 0; i < MESRForm.MitarbeiterListe.length; i++) {
  AntraegeListe.push(obj);
}
  for (var i = 0; i < MESRForm.MitarbeiterListe.length; i++) {
  for (const _antrag of AntraegeListe) {
    _antrag.Mitarbeiter = MESRForm.MitarbeiterListe[i].Mitarbeiter;
    _antrag.UserIDMitarbeiter =
      MESRForm.MitarbeiterListe[i].UserIDMitarbeiter;
    _antrag.Vorgesetzter = MESRForm.MitarbeiterListe[i].Vorgesetzter;

    console.log(_antrag);
    break;
  }
}

console.log(AntraegeListe);

the values of the objects in the console.log are changed, but the Array is not changed iteratively. The Value that's being assigned to the objects in the array coming from another array (MitarbeiterListe[I].Mitarbeiter)

my expected output would be creating 3 array objects with values of another arrays values.

The solution for this was moving the object declaration inside the for loop and create new objects in the array on every iteration.

  createMESR(MESRForm: any): void {
const AntraegeListe = new Array();
for (var i = 0; i < MESRForm.MitarbeiterListe.length; i++) {
  var obj = {....};

  obj.AntragID = '';
  obj.Timestamp = '';
  obj.Antragsteller = MESRForm.Antragsteller;
  obj.Mitarbeiter = MESRForm.MitarbeiterListe[i].Mitarbeiter;
  ... 

  AntraegeListe.push(obj);
}

console.log(AntraegeListe);}
2
  • Almost impossible to answer without seeing the input and the expected output. Commented Dec 17, 2022 at 14:37
  • This will push the same object to all slots in the array AntraegeListe.push(obj); You likely want to use reduce Commented Dec 17, 2022 at 14:37

1 Answer 1

0

map function doesn't change the original array rather it creates a new array.

you can read this answer for more information:

https://stackoverflow.com/a/62341776/20801806

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

2 Comments

Welcome to Stack Overflow! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
.map is actually not even neccassary but i tired alot of things but i always get the same result. the Array is not beeing changed.

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.