4 回答
TA贡献1877条经验 获得超1个赞
您可以使用类 Employee 来实现此目的
const size = Number(prompt("Enter the number of employees to be registered"));
class Employee {
constructor(name, id) {
this.name = name;
this.id = id;
}
}
let employees = [];
for (let i = 0; i < size; i++) {
let value = prompt("Enter employee name");
let id = prompt("Enter employee ID");
employees[i] = new Employee(value, id);
console.log(employees[i]);
}
console.log(employees);
TA贡献1848条经验 获得超2个赞
您可以将对象推送到数组
const size = Number(prompt("Enter the number of employees to be registered"));
const arr=[]
const orgObj = {};
for (let i = 0; i<=size; i++){
Object.defineProperties(orgObj, {
empName: {
value: prompt("Enter employee name"),
writable: true
},
empId: {
value: prompt("Enter employee id"),
writable: true
}
})
arr.push(orgObj)
}
console.log(arr);
TA贡献1848条经验 获得超6个赞
听起来您只想创建一个组织数组并将每个组织添加到其中?
const size = Number(prompt("Enter the number of employees to be registered"));
const orgs = [];
for (let i = 0; i < size; i++){
const orgObj = {};
Object.defineProperties(orgObj, {
empName: {
value: prompt("Enter employee name"),
writable: true,
enumerable: true
},
empId: {
value: prompt("Enter employee id"),
writable: true,
enumerable: true
}
});
orgs.push(orgObj);
}
console.log(JSON.stringify(orgs));
TA贡献1798条经验 获得超7个赞
const size = Number(prompt("Enter the number of employees to be registered"));
const orgObj = {};
for (let i = 1; i<=size; i++){
Object.defineProperties(orgObj, {
['empName'+i]: {
value: prompt("Enter employee name"),
writable: true,
enumerable:true,
},
['empId'+i]: {
value: prompt("Enter employee id"),
writable: true,
enumerable:true,
}
})
}
console.log(orgObj);
添加回答
举报