首页 > 解决方案 > 从 React Js 中的数组列表获取输入字段

问题描述

我从提到输入字段的 API 获取数组,下面是示例:

customerAttribute: Array(11)
0: {attributeId: 1, attributeName: "FirstName", attributeType: "String", isMandatory: false}
1: {attributeId: 2, attributeName: "LastName", attributeType: "String", isMandatory: false}
2: {attributeId: 3, attributeName: "DateOfBirth", attributeType: "Date", isMandatory: false}
3: {attributeId: 4, attributeName: "Email Address", attributeType: "String", isMandatory: false}
4: {attributeId: 5, attributeName: "Mobile Number", attributeType: "Number", isMandatory: false}
5: {attributeId: 6, attributeName: "Address Line 1", attributeType: "String", isMandatory: false}
6: {attributeId: 7, attributeName: "Address Line 2", attributeType: "String", isMandatory: false}
7: {attributeId: 8, attributeName: "City", attributeType: "String", isMandatory: false}

现在我已经 const an 其中提到了默认字段:

 const ABC = [
    { ...firstName, order: 1 },
    { ...lastName, order: 2 },
    { ...mobileNumber, order: 3 },
    { ...email, order: 4 },
    { ...address1, order: 5 },
    { ...address2, order: 6 },
    { ...city, order: 7 },
 ]

我只想显示 CustomerAttribute 数组列表中可用的字段

任何人都可以建议我该怎么做..?

标签: javascript

解决方案


首先,响应总是在 JSON 中,它将在数组中,它必须像下面是 for 循环,它将为您提供所需的值,例如 attributeID、attributeName

var  customerAttribute =
[ {attributeId: 1, attributeName: "FirstName", attributeType: "String", isMandatory: false},
 {attributeId: 2, attributeName: "LastName", attributeType: "String", isMandatory: false},
 {attributeId: 3, attributeName: "DateOfBirth", attributeType: "Date", isMandatory: false},
 {attributeId: 4, attributeName: "Email Address", attributeType: "String", isMandatory: false},
 {attributeId: 5, attributeName: "Mobile Number", attributeType: "Number", isMandatory: false},
 {attributeId: 6, attributeName: "Address Line 1", attributeType: "String", isMandatory: false},
 {attributeId: 7, attributeName: "Address Line 2", attributeType: "String", isMandatory: false},
 {attributeId: 8, attributeName: "City", attributeType: "String", isMandatory: false}]


    for (let index = 0; index < customerAttribute.length; index++) {

        console.log(customerAttribute[index].attributeId)
        console.log(customerAttribute[index].attributeName)

    }

推荐阅读