Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript-->;对于带对象的循环数组_Javascript_Arrays_Object_For Loop - Fatal编程技术网

Javascript-->;对于带对象的循环数组

Javascript-->;对于带对象的循环数组,javascript,arrays,object,for-loop,Javascript,Arrays,Object,For Loop,在Javascript调查的背景下,我有以下数据结构 quest_1: { quest: 'Question', answers: [ {text: 'answer 1', active: false, multiple: false}, {text: 'answer 2', active: false, multiple: false}, {text: 'answer 3', active: false, multiple: false},

在Javascript调查的背景下,我有以下数据结构

quest_1: {
  quest: 'Question',
    answers: [
      {text: 'answer 1', active: false, multiple: false},
      {text: 'answer 2', active: false, multiple: false},
      {text: 'answer 3', active: false, multiple: false},
      {text: 'answer 4', active: false, multiple: false},
    ],
    choice: [],
},
...
程序在userEvents(单击答案)过程中操纵此活动状态。 为了保存当前用户选择,我使用以下方法,通过上面的数据结构(应答数组)使用For循环:

} }

在下图中,您可以看到
对象
内部
数组
和数组内部的数据结构。 把重点放在三个方面。对象(应答)第一个console.log显示为false,数组显示为true。 为什么我不能在我的声明中达到这些对象的活动状态

我希望任何人都能给我一个建议。 提前谢谢

var sample = [{a:1, b:1}, {a:2, b:2}];
for(var index in sample){
  if(sample.hasOwnProperty(index){
    var obj = sample[index];
    console.log(obj.a + ' ' + obj.b);
  }
}

像foreach一样使用此构造

更简单的方法是使用
数组。过滤器
删除数组中不再需要的项:

pushAnswer(answers, target){
   const activeAnswers = answers.filter(answer => answer.active === true);
   target.push(...activeAnswers);
}
这很可能是一条直线:

pushAnswer(answers, target){
   target.push(...answers.filter(({active}) => active));
}
我只是把它说出来,以便更清楚。如果您没有ES6(看起来这是一个类方法,所以我假设您有):

如果只需要匹配对象的
.text
属性,可以添加额外的
.map
操作以仅选择该属性:

pushAnswer(answers, target){
   const activeText = answers.filter(({active}) => active)
                             .map(({text}) => text)
   target.push(...activeText);
}

您缺少
answers.active
answers.text
中的数组索引。替换为
答案[i]。激活的
等。感谢您的回答。我试图改变陈述并编辑了我的问题。也许您可以给我检查一下我的console.log映像。提前谢谢。我刚刚在一系列4个问题上运行了修改后的
推送答案
,第3个问题处于活动状态。它工作得很好。我假设您在周围代码中有错误,例如,仅在调用
pushAnswer
后才设置
active=true
(这仍然会导致您向我们显示的控制台输出)。感谢您提供的其他信息,问题出在周围代码中…现在可以工作了!谢谢你的回答。我用我的for循环语句的新尝试编辑了我的问题,但我不明白为什么显示的console.log无法达到此数组中的活动状态。感谢Rob的回答,这是解决此问题的明智方法,我尝试了ES6变体!你好,Rob,由于我的数据结构,我得到了正确的对象,其中包含一个active=true as数组。如何在“结果数组”中包含此筛选器的返回设置object.text的语句?(而不仅仅是相关的整个对象)-再次感谢您的支持。没问题,很乐意帮助!我更新了我的答案来解决这个问题。非常感谢你,罗布!
pushAnswer(answers, target){
   target.push(...answers.filter(({active}) => active));
}
pushAnswer(answers, target){
   var activeAnswers = answers.filter(function(answer) {
       return answer.active === true
   });
   target = target.concat(activeAnswers);
}
pushAnswer(answers, target){
   const activeText = answers.filter(({active}) => active)
                             .map(({text}) => text)
   target.push(...activeText);
}