I have array of objects and i try to create the table (tr/td) with vanilla javascript. Objects haven't the "number" key. I don't know how add the numbers to table. Github repo: https://github.com/HikkiYarik/TaskList dataUsers.js and renderUsers.js
I was try to create in getUsers()
function keys() {
const keys = Object.keys(users);
for (key in keys) {
return key;
}
}
and add keys() to newObj
users.forEach((obj) => {
const newObj = {
[map[0]]: `${obj._id}`,
[map[1]]: `${obj.name}`,
[map[2]]: `${obj.isActive}`,
[map[3]]: `${obj.balance}`,
[map[4]]: `${obj.email}`,
// [map[5]]: keys(),
};
newUsers.push(newObj);
});
and other similar methods, but no. Result: the number in table have or every zero if
function keys() {
const keys = Object.keys(users);
for (key in keys) {
return key;
}
}
or every 2(length of my array of objects) if
function keys() {
const keys = Object.keys(users);
for (key in keys) {}
return key;
}
pls help
Add index field to the object, like this
users.forEach((obj, index) => {
const newObj = {
[map[0]]: `${index + 1}`, // Add a numeric key starting at 1
[map[1]]: `${obj._id}`,
[map[2]]: `${obj.name}`,
[map[3]]: `${obj.isActive}`,
[map[4]]: `${obj.balance}`,
[map[5]]: `${obj.email}`,
};
newUsers.push(newObj;)
});
You don't need the keys() function in your source code because Object.keys(users) returns an array of keys of the users object, not an array of objects. Your goal is to add the object's sequence number to the array, not the keys from the object itself.