猿问

Javascript 中的用户定义对象

好的,下面是创建具有两个属性的用户定义对象的代码。运行良好,但值被覆盖。我如何显示用户在对象“objOrg”中输入的所有值


const size = Number(prompt("Enter the number of employees to be registered"));

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

        }

    })


}

console.log(orgObj);


繁星coding
浏览 96回答 4
4回答

冉冉说

您可以使用类 Employee 来实现此目的const size = Number(prompt("Enter the number of employees to be registered"));class Employee {&nbsp; constructor(name, id) {&nbsp; &nbsp; this.name = name;&nbsp; &nbsp; this.id = id;&nbsp; }}let employees = [];for (let i = 0; i < size; i++) {&nbsp; &nbsp; let value = prompt("Enter employee name");&nbsp; &nbsp; let id = prompt("Enter employee ID");&nbsp; &nbsp; employees[i] = new Employee(value, id);&nbsp; &nbsp; console.log(employees[i]);}console.log(employees);

慕尼黑5688855

您可以将对象推送到数组const size = Number(prompt("Enter the number of employees to be registered"));const arr=[]const orgObj = {};&nbsp; &nbsp; for (let i = 0; i<=size; i++){&nbsp; &nbsp; &nbsp; &nbsp; Object.defineProperties(orgObj, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; empName: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;value: prompt("Enter employee name"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;writable: true&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; empId: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;value: prompt("Enter employee id"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;writable: true&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; })arr.push(orgObj)}console.log(arr);

慕勒3428872

听起来您只想创建一个组织数组并将每个组织添加到其中?const size = Number(prompt("Enter the number of employees to be registered"));const orgs = [];for (let i = 0; i < size; i++){&nbsp; &nbsp; const orgObj = {};&nbsp; &nbsp; Object.defineProperties(orgObj, {&nbsp; &nbsp; &nbsp; &nbsp; empName: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: prompt("Enter employee name"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writable: true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; enumerable: true&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; empId: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: prompt("Enter employee id"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writable: true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; enumerable: true&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; orgs.push(orgObj);}console.log(JSON.stringify(orgs));

元芳怎么了

const size = Number(prompt("Enter the number of employees to be registered"));const orgObj = {};&nbsp; &nbsp; for (let i = 1; i<=size; i++){&nbsp; &nbsp; &nbsp; &nbsp; Object.defineProperties(orgObj, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ['empName'+i]: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;value: prompt("Enter employee name"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;writable: true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;enumerable:true,&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ['empId'+i]: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;value: prompt("Enter employee id"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;writable: true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;enumerable:true,&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; })}console.log(orgObj);
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答