第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在 API 響應(yīng)中迭代并可能返回多個(gè)項(xiàng)目的正確方法是什么?

在 API 響應(yīng)中迭代并可能返回多個(gè)項(xiàng)目的正確方法是什么?

拉丁的傳說 2023-10-20 10:25:08
以下是我為 SDK 創(chuàng)建的一個(gè) API 調(diào)用,該調(diào)用返回帶有各種字段的 Javascript 對(duì)象:    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;            });    }響應(yīng)如下所示,其中可能有多個(gè)項(xiàng)目。我不確定應(yīng)該如何在不硬編碼固定長(zhǎng)度循環(huán)的情況下進(jìn)行迭代,以及應(yīng)該如何相應(yīng)地構(gòu)造我的返回對(duì)象。[    {        "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,        }    }]
查看完整描述

3 回答

?
狐的傳說

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個(gè)贊

您可以循環(huán)遍歷結(jié)果


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_array = [];

        for (var i=0; i<response.data.length; i++){ //iterate

            var obj = {  //construct i-th object

                id: response.data[i].id,

                name: response.data[i].name,

                display_name: response.data[i].display_name,

                address: response.data[i].address,

                city: response.data[i].city,

                state_code: response.data[i].state_code,

                country_code: response.data[i].country_code,

                zip: response.data[i].zip,

                latitude: response.data[i].latitude,

                longitude: response.data[i].longitude

            };

            return_array.push(obj); //push object to array

        }                       

        return return_array; //return array with all the objects

    })

    .catch(function (error) {

        console.log(error);

        return error;

    });

}


查看完整回答
反對(duì) 回復(fù) 2023-10-20
?
千巷貓影

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊

從您的代碼中我可以看到您只是使用相同的鍵名稱重新分配值。因此,return_obj; 為什么不直接返回obj[0]; 已經(jīng)有鍵值對(duì)的返回,而不是返回 return 。



查看完整回答
反對(duì) 回復(fù) 2023-10-20
?
一只斗牛犬

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超2個(gè)贊

描述

您正在尋找一個(gè)for循環(huán)或一個(gè)forEach循環(huán)response.data.forEach(element => { //loop code }); ,如果使用for您想要使用的循環(huán)for (let i = 0; i < response.data.length; i++) { //loop over response.data[i] }


例子

for循環(huán)


let data = [

    {

        "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,

        "created_at": "2020-04-03T00:35:03Z",

        "updated_at": "2020-04-03T00:45:17Z",

        "active": true,

        "origin_id": null,

        "origin_data": null,

        "origin_code": null,

        "owners_project_id": null,

        "estimated_value": null,

        "project_region_id": null,

        "project_bid_type_id": null,

        "project_owner_type_id": null,

        "photo_id": 310560,

        "start_date": null,

        "completion_date": null,

        "total_value": null,

        "accounting_project_number": null,

        "store_number": null,

        "designated_market_area": null,

        "company": {

            "id": 27669,

            "name": "Example Procore App"

        }

    },

    {

        "id": 20758,

        "name": "Standard Project Template",

        "display_name": "Standard Project Template",

        "project_number": null,

        "address": null,

        "city": null,

        "state_code": null,

        "country_code": null,

        "zip": null,

        "county": null,

        "time_zone": "US/Pacific",

        "latitude": null,

        "longitude": null,

        "stage": "None",

        "phone": null,

        "created_at": "2020-04-03T00:25:02Z",

        "updated_at": "2020-04-03T00:30:01Z",

        "active": true,

        "origin_id": null,

        "origin_data": null,

        "origin_code": null,

        "owners_project_id": null,

        "estimated_value": null,

        "project_region_id": null,

        "project_bid_type_id": null,

        "project_owner_type_id": null,

        "photo_id": null,

        "start_date": null,

        "completion_date": null,

        "total_value": null,

        "accounting_project_number": null,

        "store_number": null,

        "designated_market_area": null,

        "company": {

            "id": 27669,

            "name": "Example Procore App"

        }

    }

]


for (let i = 0; i < data.length; i++) {

  console.log('index', i, data[i]);

}


foreach循環(huán)


let data = [

    {

        "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,

        "created_at": "2020-04-03T00:35:03Z",

        "updated_at": "2020-04-03T00:45:17Z",

        "active": true,

        "origin_id": null,

        "origin_data": null,

        "origin_code": null,

        "owners_project_id": null,

        "estimated_value": null,

        "project_region_id": null,

        "project_bid_type_id": null,

        "project_owner_type_id": null,

        "photo_id": 310560,

        "start_date": null,

        "completion_date": null,

        "total_value": null,

        "accounting_project_number": null,

        "store_number": null,

        "designated_market_area": null,

        "company": {

            "id": 27669,

            "name": "Example Procore App"

        }

    },

    {

        "id": 20758,

        "name": "Standard Project Template",

        "display_name": "Standard Project Template",

        "project_number": null,

        "address": null,

        "city": null,

        "state_code": null,

        "country_code": null,

        "zip": null,

        "county": null,

        "time_zone": "US/Pacific",

        "latitude": null,

        "longitude": null,

        "stage": "None",

        "phone": null,

        "created_at": "2020-04-03T00:25:02Z",

        "updated_at": "2020-04-03T00:30:01Z",

        "active": true,

        "origin_id": null,

        "origin_data": null,

        "origin_code": null,

        "owners_project_id": null,

        "estimated_value": null,

        "project_region_id": null,

        "project_bid_type_id": null,

        "project_owner_type_id": null,

        "photo_id": null,

        "start_date": null,

        "completion_date": null,

        "total_value": null,

        "accounting_project_number": null,

        "store_number": null,

        "designated_market_area": null,

        "company": {

            "id": 27669,

            "name": "Example Procore App"

        }

    }

];


data.forEach(element => {

  console.log(element);

});


查看完整回答
反對(duì) 回復(fù) 2023-10-20
  • 3 回答
  • 0 關(guān)注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)