Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.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_Angularjs - Fatal编程技术网

javascript中的元素数组

javascript中的元素数组,javascript,angularjs,Javascript,Angularjs,我想根据我拥有的数组元素生成动态按钮。 我可以生成按钮,但是我使用的数组有对象而不是数组元素。我需要使用buttons json数组来跟踪按钮状态和其他计算。你能帮我把代码改成数组元素而不是对象吗 var cars = [1,2,3,4,5,6]; $scope.btns = []; for (var i = 0; i < cars.length; ++i) { if(cars[i]!== 4 && cars[i]!==5) { $scope.btns.pus

我想根据我拥有的数组元素生成动态按钮。 我可以生成按钮,但是我使用的数组有对象而不是数组元素。我需要使用buttons json数组来跟踪按钮状态和其他计算。你能帮我把代码改成数组元素而不是对象吗

var cars = [1,2,3,4,5,6];
$scope.btns = [];
 for (var i = 0; i < cars.length; ++i) {
 if(cars[i]!== 4 && cars[i]!==5)
  {
   $scope.btns.push({label: cars[i]+"/0/0", state: false });
   $scope.btns.push({label: cars[i]+"/0/1", state: false });
  }
}
console.log($scope.btns);

开发工具没有向您展示序列化的JSON,因为它具有更强大的功能。如果您想查看数组的JSON表示形式,可以使用
console.log(JSON.stringify($scope.btns)),但这在很大程度上是不必要的,因为您已经有了适当的结构。

通过使用{…},您正在创建“对象”。。不是纽扣。你是说一个HTML按钮吗?$scope.btns到底是什么?也许是因为你把对象放进去了,这只是一个猜测。@Bálint哈哈。您试图通过生成的阵列实现什么?您在这里得到了预期的输出?从我看到的情况来看,您正在放置对象,因此输出是正确的。你能解释一下你的最终目标吗?我需要根据我从rest通话中得到的服务列表显示按钮。用户将以按钮格式显示选项列表。按钮将具有选择或取消选择的切换功能。谢谢ZZBOV。我能看到我想要什么。
current output:
 [Object { label="1/0/0",  state=false}, Object { label="1/0/1",  state=false}, Object { label="2/0/0",  state=false}, Object { label="2/0/1",  state=false}, Object { label="3/0/0",  state=false}, Object { label="3/0/1",  state=false}, Object { label="6/0/0",  state=false}, Object { label="6/0/1",  state=false}]

Expected:
 [{ label="1/0/0",  state=false}, { label="1/0/1",  state=false}, { label="2/0/0",  state=false}, { label="2/0/1",  state=false}, { label="3/0/0",  state=false}, { label="3/0/1",  state=false}, { label="6/0/0",  state=false}, { label="6/0/1",  state=false}]