I am dealing with an API that returns a JSON Array of objects in the following format:
[
{
person_id:"12212",
person_name:"some name",
deptt : "dept-1"
},
{
person_id: "12211",
person_name: "some name 2"
deptt : "dept-2"
},
]
My issue is related to saving the person_id values into an array. For some reason, I am not able to save them correctly and thus the length of the array is showing incorrect.
Using Chakram for this task, this is my current approach:
it('gets person id in array',()=>{
let array_ids =[];
let count=0;
return response.then((api_response)=>{
for(var i=1;i<api_response.body.length;i++){
//this is correctly printing the person id of response
console.log('Person ids are ==>'+api_response.body[i].person_id);
count++;
//this is not working
array_ids = api_response.body[i].person_id;
}
console.log('Count is '+count) //prints correct number
console.log('Array length '+array_ids.length) //prints incorrect length - sometimes 11, sometimes 12
});
});
I am puzzled as to why this discrepancy is happening. Could it be due to this line?
array_ids = api_response.body[i].person_id
Is this the wrong way of accessing elements in an array?