如何遍历包含其他对象的JavaScript对象并从数组分配值

时间:2019-07-04 13:28:55

标签: arrays javascript-objects

我正在编写处理如下定义的名为createdCustomer的对象的代码

createdCustomer: {
          email: "string",
          phoneNumber: "string",
          mobileNumber : "string",
          temporaryAddress : {
              address1: "string",
              address2: "string",
              address3: "string",
              address4: "string",
              city: "string",
              country: "string",
              region: "string",
              zipCode: "string"
          },
          customerNumber: "string",
          locale: "string",
          regNo: "string",
          address: {
            address1: "string",
            address2: "string",
            address3: "string",
            address4: "string",
            city: "string",
            country: "string",
            region: "string",
            zipCode: "string"
          },
          salutation: "string",
          firstName: "string",
          middleName: "string",
          lastName: "string"
        }

,并尝试从大小为26的名为formValues的数组中分配值,该数组与createdCustomer中的字符串数相同。

到目前为止,我已经设法分配了该数组的所有值,

this.createdCustomer.email = formValues[0];
this.createdCustomer.phoneNumber = formValues[1];
this.createdCustomer.mobileNumber = formValues[2];
this.createdCustomer.temporaryAddress.address1 = formValues[3];
this.createdCustomer.temporaryAddress.address2 = formValues[4];
this.createdCustomer.temporaryAddress.address3 = formValues[5];
this.createdCustomer.temporaryAddress.address4 = formValues[6];
this.createdCustomer.temporaryAddress.city = formValues[7];
this.createdCustomer.temporaryAddress.country = formValues[8];
this.createdCustomer.temporaryAddress.region = formValues[9];
this.createdCustomer.temporaryAddress.zipCode = formValues[10];
this.createdCustomer.customerNumber = formValues[11];
this.createdCustomer.locale = formValues[12];
this.createdCustomer.regNo = formValues[13];
this.createdCustomer.address.address1 = formValues[14];
this.createdCustomer.address.address2 = formValues[15];
this.createdCustomer.address.address3 = formValues[16];
this.createdCustomer.address.address4 = formValues[17];
this.createdCustomer.address.city = formValues[18];
this.createdCustomer.address.country = formValues[19];
this.createdCustomer.address.region = formValues[20];
this.createdCustomer.address.zipCode = formValues[21];
this.createdCustomer.salutation = formValues[22];
this.createdCustomer.firstName = formValues[23];
this.createdCustomer.middleName = formValues[24];
this.createdCustomer.lastName = formValues[25];

,但是由于这几乎是硬编码和冗余的,所以我想使用循环来重构它。

当我研究如何遍历包含另一个对象的JavaScript对象时,发现的页面仅解释了如何遍历“直截了当”的对象,例如

myFriends : {
    {
        name: "John",
        age: 23,
        homeTown: "New York"
    },
    {
        name: "Anna",
        age: 25,
        homeTown: "San Diego"
    },
    {
        name: "Julian",
        age: 20,
        homeTown: "London"
    }
}

上面的示例很容易遍历,因为所有3个对象都是“相同形状”。但是我处理createdCustomer的情况可能有点棘手,因为有时您会遇到一个简单的字符串,有时会遇到一个由多个字符串等组成的对象。

此外,您还需要同时遍历数组formValues,并将此数组的每个值分配给createdCustomer以更新数据。

如果您要使用循环来表达与我如何分配数据的硬编码版本相同的内容,您将如何处理?

如果您只想搜索/更新一个值,则可以很容易地用地图处理,但是在这种情况下,我认为它运行得并不顺利...

0 个答案:

没有答案
相关问题