5
I am working on the development of a dynamic table and I am facing problems to create the rows of this table according to the columns I have and the information I receive from the API.
Considering the following code:
let data = [];
let line = {};
const columns = [{
name: "Code",
dataIndex: "id"
}, {
name: "Name",
dataIndex: "name"
}];
const arrFromApi = [{
id: 1,
name: "Marcus",
age: "32"
}, {
id: 2,
name: "John",
age: "30"
}, {
id: 3,
name: "Emily",
age: "25"
}];
arrFromApi.forEach(item => {
columns.forEach(column => {
line[column.dataIndex] = item[column.dataIndex];
})
data.push(line)
});
console.log(data)
I have the following result:
[{id: 3, name: "Emily"}, {id: 3, name: "Emily"}, {id: 3, name: "Emily"}]
But if I replace my data.push(line)
for console.log(line)
is printed on the console correctly each of the names as needed:
[{id: 1, name: "Marcus"}, {id: 2, name: "John"}, {id: 3, name: "Emily"}]
Would anyone know what’s wrong with this scenario? Because I understand that if the console is printing in this sequence, then the end result of my array should be the same.