简体   繁体   中英

how to iterate complex object from the api response

This is an api response and I want to iterate all the appId and userInfo from the response. How can i iterate over this response?

{
    "status_code": "SUCCESS",
    "status": "SUCCESS",
    "message": "Success",
    "data": [
        {
            "_id": "6034acaaf751f1c89721a76b",
            "status": "active",
            "name": "testname",
            "collectionStatus": "completed",
            "createdDate": 1614064810201,
            "subscriberStats": [],
            "platforms": [
                {
                    "_id": "6034acaa47a83e45b3f5fc4a",
                    "status": "active",
                    "traffic": 0,
                    "parentStatus": "active",
                    "platformType": "website",
                    "appId": "xxx",
                    "optinShowInterval": 0,
                    "optInFrequencyType": "page_refresh",
                    "webType": "wordpress",
                    "optInFrequency": 0,
                    "userId": "xxx",
                    "createdDate": 1614064810563
                }
            ]
        },
        {
            "_id": "600ae310255964c2e21a580f",
            "status": "active",
            "name": "trial",
            "collectionStatus": "completed",
            "createdDate": 1611326224761,
            "subscriberStats": [],
            "platforms": [
                {
                    "_id": "600ae31163d6d321f4693d1d",
                    "status": "active",
                    "traffic": 0,
                    "parentStatus": "active",
                    "platformType": "website",
                    "webType": "wordpress",
                    "appId": "xxx",
                    "optinShowInterval": 0,
                    "optInFrequencyType": "page_refresh",
                    "optInFrequency": 0,
                    "userId": "xxx",
                    "createdDate": 1611326225013
                }
            ]
        }
    ]
}

the resulting output should be an array of objects, for eg:

[
   {appId: "", userInfo: ""},
   {appId: "", userInfo: ""}
]

Try adding some loops:

const result = [];
for (const a in response) {
  for (const b of response[a]) {
    result.push({
      appId: b._id,
      userInfo: b.parentStatus // or some other properties
    })
  }
}

console.log(result)

appId and usetId is nested deep inside object inside platforms array you can access them by using following code

 const res = { status_code: "SUCCESS", status: "SUCCESS", message: "Success", data: [ { _id: "6034acaaf751f1c89721a76b", status: "active", name: "testname", collectionStatus: "completed", createdDate: 1614064810201, subscriberStats: [], platforms: [ { _id: "6034acaa47a83e45b3f5fc4a", status: "active", traffic: 0, parentStatus: "active", platformType: "website", appId: "xxx", optinShowInterval: 0, optInFrequencyType: "page_refresh", webType: "wordpress", optInFrequency: 0, userId: "xxx", createdDate: 1614064810563, }, ], }, { _id: "600ae310255964c2e21a580f", status: "active", name: "trial", collectionStatus: "completed", createdDate: 1611326224761, subscriberStats: [], platforms: [ { _id: "600ae31163d6d321f4693d1d", status: "active", traffic: 0, parentStatus: "active", platformType: "website", webType: "wordpress", appId: "xxx", optinShowInterval: 0, optInFrequencyType: "page_refresh", optInFrequency: 0, userId: "xxx", createdDate: 1611326225013, }, ], }, ], }; const output = res.data.map((obj) => { return { appId: obj.platforms[0].appId, userId: obj.platforms[0].userId }; }); console.log(output);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM