猿问

在 API 响应中迭代并可能返回多个项目的正确方法是什么?

以下是我为 SDK 创建的一个 API 调用,该调用返回带有各种字段的 Javascript 对象:


    listProjects(company_id) {

        return axios.get(BASE_URL + 'projects?company_id=' + company_id, {

            headers: {

                Authorization: this.access_token

            }

        })

            .then(function (response) {

                //console.log(response.data);

                const obj = response.data;

                var return_obj = { //needs to return data for all the projects not just the first

                    id: obj[0].id,

                    name: obj[0].name,

                    display_name: obj[0].display_name,

                    address: obj[0].address,

                    city: obj[0].city,

                    state_code: obj[0].state_code,

                    country_code: obj[0].country_code,

                    zip: obj[0].zip,

                    latitude: obj[0].latitude,

                    longitude: obj[0].longitude

                };

                return return_obj;

            })

            .catch(function (error) {

                console.log(error);

                return error;

            });

    }

响应如下所示,其中可能有多个项目。我不确定应该如何在不硬编码固定长度循环的情况下进行迭代,以及应该如何相应地构造我的返回对象。


[

    {

        "id": 20789,

        "name": "Sandbox Test Project",

        "display_name": "1234 - Sandbox Test Project",

        "project_number": "1234",

        "address": "6309 Carpinteria Avenue",

        "city": "Carpinteria",

        "state_code": "CA",

        "country_code": "US",

        "zip": "93013",

        "county": "Santa Barbara County",

        "time_zone": "US/Pacific",

        "latitude": 34.3850438,

        "longitude": -119.4908492,

        "stage": "None",

        "phone": null,

        }

    }

]


拉丁的传说
浏览 128回答 3
3回答

狐的传说

您可以循环遍历结果listProjects(company_id) {return axios.get(BASE_URL + 'projects?company_id=' + company_id, {&nbsp; &nbsp; headers: {&nbsp; &nbsp; &nbsp; &nbsp; Authorization: this.access_token&nbsp; &nbsp; }})&nbsp; &nbsp; .then(function (response) {&nbsp; &nbsp; &nbsp; &nbsp; //console.log(response.data);&nbsp; &nbsp; &nbsp; &nbsp; //const obj = response.data;&nbsp; &nbsp; &nbsp; &nbsp; var return_array = [];&nbsp; &nbsp; &nbsp; &nbsp; for (var i=0; i<response.data.length; i++){ //iterate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var obj = {&nbsp; //construct i-th object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: response.data[i].id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: response.data[i].name,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display_name: response.data[i].display_name,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; address: response.data[i].address,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; city: response.data[i].city,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state_code: response.data[i].state_code,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; country_code: response.data[i].country_code,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zip: response.data[i].zip,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; latitude: response.data[i].latitude,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; longitude: response.data[i].longitude&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return_array.push(obj); //push object to array&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return return_array; //return array with all the objects&nbsp; &nbsp; })&nbsp; &nbsp; .catch(function (error) {&nbsp; &nbsp; &nbsp; &nbsp; console.log(error);&nbsp; &nbsp; &nbsp; &nbsp; return error;&nbsp; &nbsp; });}

千巷猫影

从您的代码中我可以看到您只是使用相同的键名称重新分配值。因此,return_obj;&nbsp;为什么不直接返回obj[0];&nbsp;已经有键值对的返回,而不是返回 return 。

一只斗牛犬

描述您正在寻找一个for循环或一个forEach循环response.data.forEach(element => { //loop code }); ,如果使用for您想要使用的循环for (let i = 0; i < response.data.length; i++) { //loop over response.data[i] }例子for循环let data = [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": 20789,&nbsp; &nbsp; &nbsp; &nbsp; "name": "Sandbox Test Project",&nbsp; &nbsp; &nbsp; &nbsp; "display_name": "1234 - Sandbox Test Project",&nbsp; &nbsp; &nbsp; &nbsp; "project_number": "1234",&nbsp; &nbsp; &nbsp; &nbsp; "address": "6309 Carpinteria Avenue",&nbsp; &nbsp; &nbsp; &nbsp; "city": "Carpinteria",&nbsp; &nbsp; &nbsp; &nbsp; "state_code": "CA",&nbsp; &nbsp; &nbsp; &nbsp; "country_code": "US",&nbsp; &nbsp; &nbsp; &nbsp; "zip": "93013",&nbsp; &nbsp; &nbsp; &nbsp; "county": "Santa Barbara County",&nbsp; &nbsp; &nbsp; &nbsp; "time_zone": "US/Pacific",&nbsp; &nbsp; &nbsp; &nbsp; "latitude": 34.3850438,&nbsp; &nbsp; &nbsp; &nbsp; "longitude": -119.4908492,&nbsp; &nbsp; &nbsp; &nbsp; "stage": "None",&nbsp; &nbsp; &nbsp; &nbsp; "phone": null,&nbsp; &nbsp; &nbsp; &nbsp; "created_at": "2020-04-03T00:35:03Z",&nbsp; &nbsp; &nbsp; &nbsp; "updated_at": "2020-04-03T00:45:17Z",&nbsp; &nbsp; &nbsp; &nbsp; "active": true,&nbsp; &nbsp; &nbsp; &nbsp; "origin_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "origin_data": null,&nbsp; &nbsp; &nbsp; &nbsp; "origin_code": null,&nbsp; &nbsp; &nbsp; &nbsp; "owners_project_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "estimated_value": null,&nbsp; &nbsp; &nbsp; &nbsp; "project_region_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "project_bid_type_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "project_owner_type_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "photo_id": 310560,&nbsp; &nbsp; &nbsp; &nbsp; "start_date": null,&nbsp; &nbsp; &nbsp; &nbsp; "completion_date": null,&nbsp; &nbsp; &nbsp; &nbsp; "total_value": null,&nbsp; &nbsp; &nbsp; &nbsp; "accounting_project_number": null,&nbsp; &nbsp; &nbsp; &nbsp; "store_number": null,&nbsp; &nbsp; &nbsp; &nbsp; "designated_market_area": null,&nbsp; &nbsp; &nbsp; &nbsp; "company": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": 27669,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "Example Procore App"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": 20758,&nbsp; &nbsp; &nbsp; &nbsp; "name": "Standard Project Template",&nbsp; &nbsp; &nbsp; &nbsp; "display_name": "Standard Project Template",&nbsp; &nbsp; &nbsp; &nbsp; "project_number": null,&nbsp; &nbsp; &nbsp; &nbsp; "address": null,&nbsp; &nbsp; &nbsp; &nbsp; "city": null,&nbsp; &nbsp; &nbsp; &nbsp; "state_code": null,&nbsp; &nbsp; &nbsp; &nbsp; "country_code": null,&nbsp; &nbsp; &nbsp; &nbsp; "zip": null,&nbsp; &nbsp; &nbsp; &nbsp; "county": null,&nbsp; &nbsp; &nbsp; &nbsp; "time_zone": "US/Pacific",&nbsp; &nbsp; &nbsp; &nbsp; "latitude": null,&nbsp; &nbsp; &nbsp; &nbsp; "longitude": null,&nbsp; &nbsp; &nbsp; &nbsp; "stage": "None",&nbsp; &nbsp; &nbsp; &nbsp; "phone": null,&nbsp; &nbsp; &nbsp; &nbsp; "created_at": "2020-04-03T00:25:02Z",&nbsp; &nbsp; &nbsp; &nbsp; "updated_at": "2020-04-03T00:30:01Z",&nbsp; &nbsp; &nbsp; &nbsp; "active": true,&nbsp; &nbsp; &nbsp; &nbsp; "origin_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "origin_data": null,&nbsp; &nbsp; &nbsp; &nbsp; "origin_code": null,&nbsp; &nbsp; &nbsp; &nbsp; "owners_project_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "estimated_value": null,&nbsp; &nbsp; &nbsp; &nbsp; "project_region_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "project_bid_type_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "project_owner_type_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "photo_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "start_date": null,&nbsp; &nbsp; &nbsp; &nbsp; "completion_date": null,&nbsp; &nbsp; &nbsp; &nbsp; "total_value": null,&nbsp; &nbsp; &nbsp; &nbsp; "accounting_project_number": null,&nbsp; &nbsp; &nbsp; &nbsp; "store_number": null,&nbsp; &nbsp; &nbsp; &nbsp; "designated_market_area": null,&nbsp; &nbsp; &nbsp; &nbsp; "company": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": 27669,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "Example Procore App"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }]for (let i = 0; i < data.length; i++) {&nbsp; console.log('index', i, data[i]);}foreach循环let data = [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": 20789,&nbsp; &nbsp; &nbsp; &nbsp; "name": "Sandbox Test Project",&nbsp; &nbsp; &nbsp; &nbsp; "display_name": "1234 - Sandbox Test Project",&nbsp; &nbsp; &nbsp; &nbsp; "project_number": "1234",&nbsp; &nbsp; &nbsp; &nbsp; "address": "6309 Carpinteria Avenue",&nbsp; &nbsp; &nbsp; &nbsp; "city": "Carpinteria",&nbsp; &nbsp; &nbsp; &nbsp; "state_code": "CA",&nbsp; &nbsp; &nbsp; &nbsp; "country_code": "US",&nbsp; &nbsp; &nbsp; &nbsp; "zip": "93013",&nbsp; &nbsp; &nbsp; &nbsp; "county": "Santa Barbara County",&nbsp; &nbsp; &nbsp; &nbsp; "time_zone": "US/Pacific",&nbsp; &nbsp; &nbsp; &nbsp; "latitude": 34.3850438,&nbsp; &nbsp; &nbsp; &nbsp; "longitude": -119.4908492,&nbsp; &nbsp; &nbsp; &nbsp; "stage": "None",&nbsp; &nbsp; &nbsp; &nbsp; "phone": null,&nbsp; &nbsp; &nbsp; &nbsp; "created_at": "2020-04-03T00:35:03Z",&nbsp; &nbsp; &nbsp; &nbsp; "updated_at": "2020-04-03T00:45:17Z",&nbsp; &nbsp; &nbsp; &nbsp; "active": true,&nbsp; &nbsp; &nbsp; &nbsp; "origin_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "origin_data": null,&nbsp; &nbsp; &nbsp; &nbsp; "origin_code": null,&nbsp; &nbsp; &nbsp; &nbsp; "owners_project_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "estimated_value": null,&nbsp; &nbsp; &nbsp; &nbsp; "project_region_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "project_bid_type_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "project_owner_type_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "photo_id": 310560,&nbsp; &nbsp; &nbsp; &nbsp; "start_date": null,&nbsp; &nbsp; &nbsp; &nbsp; "completion_date": null,&nbsp; &nbsp; &nbsp; &nbsp; "total_value": null,&nbsp; &nbsp; &nbsp; &nbsp; "accounting_project_number": null,&nbsp; &nbsp; &nbsp; &nbsp; "store_number": null,&nbsp; &nbsp; &nbsp; &nbsp; "designated_market_area": null,&nbsp; &nbsp; &nbsp; &nbsp; "company": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": 27669,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "Example Procore App"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": 20758,&nbsp; &nbsp; &nbsp; &nbsp; "name": "Standard Project Template",&nbsp; &nbsp; &nbsp; &nbsp; "display_name": "Standard Project Template",&nbsp; &nbsp; &nbsp; &nbsp; "project_number": null,&nbsp; &nbsp; &nbsp; &nbsp; "address": null,&nbsp; &nbsp; &nbsp; &nbsp; "city": null,&nbsp; &nbsp; &nbsp; &nbsp; "state_code": null,&nbsp; &nbsp; &nbsp; &nbsp; "country_code": null,&nbsp; &nbsp; &nbsp; &nbsp; "zip": null,&nbsp; &nbsp; &nbsp; &nbsp; "county": null,&nbsp; &nbsp; &nbsp; &nbsp; "time_zone": "US/Pacific",&nbsp; &nbsp; &nbsp; &nbsp; "latitude": null,&nbsp; &nbsp; &nbsp; &nbsp; "longitude": null,&nbsp; &nbsp; &nbsp; &nbsp; "stage": "None",&nbsp; &nbsp; &nbsp; &nbsp; "phone": null,&nbsp; &nbsp; &nbsp; &nbsp; "created_at": "2020-04-03T00:25:02Z",&nbsp; &nbsp; &nbsp; &nbsp; "updated_at": "2020-04-03T00:30:01Z",&nbsp; &nbsp; &nbsp; &nbsp; "active": true,&nbsp; &nbsp; &nbsp; &nbsp; "origin_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "origin_data": null,&nbsp; &nbsp; &nbsp; &nbsp; "origin_code": null,&nbsp; &nbsp; &nbsp; &nbsp; "owners_project_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "estimated_value": null,&nbsp; &nbsp; &nbsp; &nbsp; "project_region_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "project_bid_type_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "project_owner_type_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "photo_id": null,&nbsp; &nbsp; &nbsp; &nbsp; "start_date": null,&nbsp; &nbsp; &nbsp; &nbsp; "completion_date": null,&nbsp; &nbsp; &nbsp; &nbsp; "total_value": null,&nbsp; &nbsp; &nbsp; &nbsp; "accounting_project_number": null,&nbsp; &nbsp; &nbsp; &nbsp; "store_number": null,&nbsp; &nbsp; &nbsp; &nbsp; "designated_market_area": null,&nbsp; &nbsp; &nbsp; &nbsp; "company": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": 27669,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "Example Procore App"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }];data.forEach(element => {&nbsp; console.log(element);});
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答