React native中的JSON对象渲染

nszi6y05  于 2023-11-20  发布在  React
关注(0)|答案(1)|浏览(110)

我想知道是否可以创建一个节调用group_id,并在react-native中列出该节中group_id中的所有成员
我尝试过使用for循环,但它不能正常工作。这是我目前正在使用的JSON对象。理想情况下,我希望group_id和组中的成员一起列出。任何关于我应该如何开始的建议都很好。

{
  "groups": [
    {
      "group_id": 736,
      "members": [
        {
          "user_id": 1,
          "email": "[email protected]",
          "first_name": "John",
          "last_name": "Doe"
        },
        {
          "user_id": 14,
          "email": "[email protected]",
          "first_name": "Be",
          "last_name": "Hg"
        },
        {
          "user_id": 10,
          "email": "[email protected]",
          "first_name": "Sugar",
          "last_name": "Tea"
        }
      ]
    },
    {
      "group_id": 737,
      "members": [
        {
          "user_id": 3,
          "email": "[email protected]",
          "first_name": "Kent",
          "last_name": "Torrel"
        },
        {
          "user_id": 12,
          "email": "Chh",
          "first_name": "Dog",
          "last_name": "Go"
        },
        {
          "user_id": 15,
          "email": "",
          "first_name": "",
          "last_name": ""
        }
      ]
    },
    {
      "group_id": 738,
      "members": [
        {
          "user_id": 6,
          "email": "[email protected]",
          "first_name": "Raj",
          "last_name": "Parikh"
        },
        {
          "user_id": 2,
          "email": "[email protected]",
          "first_name": "Vathana",
          "last_name": "Him"
        },
        {
          "user_id": 11,
          "email": "[email protected]",
          "first_name": "Sugar",
          "last_name": "Tea"
        }
      ]
    },
    {
      "group_id": 739,
      "members": [
        {
          "user_id": 13,
          "email": "Hdh",
          "first_name": "Ou",
          "last_name": "Hm"
        },
        {
          "user_id": 9,
          "email": "[email protected]",
          "first_name": "Larry",
          "last_name": "Jones"
        },
        {
          "user_id": 4,
          "email": "[email protected]",
          "first_name": "Megan",
          "last_name": "Cruz"
        }
      ]
    },
    {
      "group_id": 740,
      "members": [
        {
          "user_id": 8,
          "email": "[email protected]",
          "first_name": "Jajsdjao",
          "last_name": "Doe"
        },
        {
          "user_id": 16,
          "email": "",
          "first_name": "",
          "last_name": ""
        },
        {
          "user_id": 5,
          "email": "[email protected]",
          "first_name": "Liron",
          "last_name": "Benjamin"
        }
      ]
    }
  ]
}

字符串
举个例子会有帮助,因为我不知道从哪里开始!

55ooxyrt

55ooxyrt1#

你可以使用for each循环

const data = {
            "groups": [
              {
                "group_id": 736,
                "members": [
                  {
                    "user_id": 1,
                    "email": "[email protected]",
                    "first_name": "John",
                    "last_name": "Doe"
                  },
                  {
                    "user_id": 14,
                    "email": "[email protected]",
                    "first_name": "Be",
                    "last_name": "Hg"
                  },
                  {
                    "user_id": 10,
                    "email": "[email protected]",
                    "first_name": "Sugar",
                    "last_name": "Tea"
                  }
                ]
              },
              {
                "group_id": 737,
                "members": [
                  {
                    "user_id": 3,
                    "email": "[email protected]",
                    "first_name": "Kent",
                    "last_name": "Torrel"
                  },
                  {
                    "user_id": 12,
                    "email": "Chh",
                    "first_name": "Dog",
                    "last_name": "Go"
                  },
                  {
                    "user_id": 15,
                    "email": "",
                    "first_name": "",
                    "last_name": ""
                  }
                ]
              },
              {
                "group_id": 738,
                "members": [
                  {
                    "user_id": 6,
                    "email": "[email protected]",
                    "first_name": "Raj",
                    "last_name": "Parikh"
                  },
                  {
                    "user_id": 2,
                    "email": "[email protected]",
                    "first_name": "Vathana",
                    "last_name": "Him"
                  },
                  {
                    "user_id": 11,
                    "email": "[email protected]",
                    "first_name": "Sugar",
                    "last_name": "Tea"
                  }
                ]
              },
              {
                "group_id": 739,
                "members": [
                  {
                    "user_id": 13,
                    "email": "Hdh",
                    "first_name": "Ou",
                    "last_name": "Hm"
                  },
                  {
                    "user_id": 9,
                    "email": "[email protected]",
                    "first_name": "Larry",
                    "last_name": "Jones"
                  },
                  {
                    "user_id": 4,
                    "email": "[email protected]",
                    "first_name": "Megan",
                    "last_name": "Cruz"
                  }
                ]
              },
              {
                "group_id": 740,
                "members": [
                  {
                    "user_id": 8,
                    "email": "[email protected]",
                    "first_name": "Jajsdjao",
                    "last_name": "Doe"
                  },
                  {
                    "user_id": 16,
                    "email": "",
                    "first_name": "",
                    "last_name": ""
                  },
                  {
                    "user_id": 5,
                    "email": "[email protected]",
                    "first_name": "Liron",
                    "last_name": "Benjamin"
                  }
                ]
              }
            ]
          }

        let jsonData= JSON.stringify(data)
        jsonData = JSON.parse(jsonData)

        let groupID=740

        jsonData.groups.forEach(element => {
            if(element.group_id === groupID){
                console.log("Group ID "+element.group_id)

                element.members.forEach(subElement =>{
                    console.log(subElement)
                })
            }
        });

字符串

相关问题