Upon receiving a JSON response from an API, the structure appears as follows:
{
"status": "success",
"response": [
{
"id": 1,
"name": "SEA BUSES",
"image": null
},
{
"id": 2,
"name": "BEN BUSES",
"image": null
},
{
"id": 3,
"name": "CAPE BUSES",
"image": null
}
]
}
The objective here is to create an array of IDs in the format ids = [1, 2, 3]
Here is the JavaScript code snippet being used:
companyid = response.data.response
var ids = [];
for (var i = 0; i < companyid.length; i++){
ids.push(companyid[i].id)
console.log(ids)
}
However, the output is not matching expectations. The current output is displayed as:
[ 1 ]
[ 1, 2 ]
[ 1, 2, 3 ]
If anyone has any insights or suggestions, your help would be greatly appreciated.