I have created an array with six objects each of which has three properties, name, age and profession. I have given them initial values. Now I have three other arrays, each having six values (six different names, six different ages and six different professions). I am trying to change the property values of objects in the initial array by looping through the other three arrays having different values for name age and profession. I have tried doing it in a single for loop, a single while loop and by using three different loops. but It is not working. the console.table() method is printing the values of last iteration of loop in all table rows (name:supermentalist, age:40 and profession:theTwo).
let arr = new Array(6).fill({});
for (ele of arr) {
ele.name = "withoutCape";
ele.age = 40;
ele.profession = "programmer";
}
console.table(arr);
const names = [
"superhero",
"superzero",
"supermum",
"superdumb",
"superscientist",
"supermentalist",
];
const ages = [35, 36, 37, 38, 39, 40];
const professions = [
"programmer",
"scientist",
"cryptographer",
"etymologist",
"theOne",
"theTwo",
];
for (let i = 0; i < professions.length; i++) {
arr[i]["name"] = names[i];
}
for (let i = 0; i < professions.length; i++) {
arr[i]["age"] = ages[i];
}
for (let i = 0; i < professions.length; i++) {
arr[i].profession = professions[i];
}
console.table(arr);