0

Here is one API call I created for an SDK that returns a Javascript object with various fields:

    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;
            });
    }

The response looks like the following where there could possibly be multiple projects. I'm not sure how I should iterate through without hard coding a fixed length loop and how I should structure my return object accordingly.

[
    {
        "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"
        }
    }
]
Albert Vu
  • 45
  • 4

3 Answers3

1

You could loop through the results

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;
    });

}

0

From your code I can see that you are just reassigning the value with the same key names. So instead of returning return return_obj; Why dont you simply return obj[0]; Which already have key value pairs.

kumar
  • 708
  • 4
  • 15
  • 39
0

Description

You are looking for a for loop or a forEach loop on response.data.forEach(element => { //loop code }); if using a for loop you'd want to use for (let i = 0; i < response.data.length; i++) { //loop over response.data[i] }

Examples

for loop

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 loop

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);
});

References

abc123
  • 17,855
  • 7
  • 52
  • 82