Javascript 保存JSON对象键时数组长度和数组元素不正确

Javascript 保存JSON对象键时数组长度和数组元素不正确,javascript,json,web-api-testing,chakram,Javascript,Json,Web Api Testing,Chakram,我有一个带有JSON对象数组的API,响应如下 [ { person_id:"12212", person_name:"some name", deptt : "dept-1" }, { person_id: "12211", person_name: "some name 2" deptt : "dept-2" }, ] 我试图将person_id的值保存在数组中,但没有正确保存这些值,因此数组长度不正

我有一个带有JSON对象数组的API,响应如下

[
   {
     person_id:"12212",
     person_name:"some name",
     deptt : "dept-1"
   },
   { 
     person_id: "12211",
     person_name: "some name 2"
     deptt : "dept-2"
    },

]
我试图将person_id的值保存在数组中,但没有正确保存这些值,因此数组长度不正确

因为我在使用脉轮,这就是我目前正在做的

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

获取/分配数组中元素的错误方法

您需要将ID推入阵列

array_ids.push(api_response.body[i].person_id);
您可以使用
Array.prototype.map()


您需要将ID推入阵列中

array_ids.push(api_response.body[i].person_id);
您可以使用
Array.prototype.map()


试试这个,代码中有一个更正。您不是将ID推入数组,而是每次都为ID分配一个新值

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.push(api_response.body[i].person_id); //update
}
      console.log('Count is '+count) //prints correct number
      console.log('Array length '+array_ids.length) //prints incorrect length - sometimes 11, sometimes 12
});
});
it('get person id in array',()=>{
让数组_id=[];
让计数=0;
返回响应。然后((api_响应)=>{

对于(var i=1;i试试这个,你的代码中有一个修正。你不是把ID放入数组,而是每次都分配一个新的ID值

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.push(api_response.body[i].person_id); //update
}
      console.log('Count is '+count) //prints correct number
      console.log('Array length '+array_ids.length) //prints incorrect length - sometimes 11, sometimes 12
});
});
it('get person id in array',()=>{
让数组_id=[];
让计数=0;
返回响应。然后((api_响应)=>{

对于(var i=1;你为什么要使用chakram?你可以用普通的方式来代替。我的API框架是在chakram中构建的。这是我想作为测试用例断言的一部分来做的事情。你为什么要使用chakram?你可以用普通的方式代替。我的API框架是在chakram中构建的。这是我想作为测试用例a的一部分来做的事情S投票。你应该解释你改变了什么以及为什么。@demouser123在我的意见代码中只有答案没有多大帮助(给鱼和教鱼)。此外,我评论并计划观看和删除我的投票,如果它被编辑。你应该解释你改变了什么以及为什么。@demouser123在我的意见代码中只有答案没有多大帮助(给鱼和教鱼)。此外,我评论并计划观看并删除我的投票,如果它被编辑的话。