I have an API with a JSON Array of objects response like this
[
{
person_id:"12212",
person_name:"some name",
deptt : "dept-1"
},
{
person_id: "12211",
person_name: "some name 2"
deptt : "dept-2"
},
]
I am trying to save the values of person_id in an array but the values are not getting saved correctly and so the array length is coming incorrect.
Since I'm using Chakram, this is what I'm currently doing
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 want to know why is this happening? Is
array_ids = api_response.body[i].person_id
an incorrect way of getting/assigning elements in array?