我正在开发angularjs中的web应用程序。我有一个foreach循环来遍历范围对象。下面是我的js代码。
$scope.details = [];
$scope.apply = function() {
angular.forEach($scope.screenMap, function(value, key) {
//$scope.details.push(value.scrn_id, value.Read, value.Write);
$scope.details.push("ID", value.scrn_id);
$scope.details.push("Read", value.Read);
$scope.details.push("Write", value.Write);
});
console.log($scope.details);
}
循环后,我总共得到18个条目,共3次迭代。
我期待的是
ID:1 Read:True Write: False
ID:2 Read:False Write: True
ID:3 Read:True Write: False
ID:4 Read:False Write: True
我可以知道如何以上述格式获取数组吗?
答案 0 :(得分:2)
您必须将值作为对象推送到数组才能实现此目的。
$scope.details = [];
$scope.apply = function () {
angular.forEach($scope.screenMap, function (value, key) {
//$scope.details.push(value.scrn_id, value.Read, value.Write);
$scope.details.push({"ID":value.scrn_id,"Read":value.Read,"Write":value.Write});
});
console.log($scope.details);
}
答案 1 :(得分:1)
试试此代码
$scope.details = [];
$scope.apply = function () {
angular.forEach($scope.screenMap, function (value, key) {
var obj = {
"ID" : value.scrn_id,
"Read" : value.Read,
"Write" : value.Write
};
$scope.details.push(obj);
});
console.log($scope.details);
}
答案 2 :(得分:1)
将其作为对象推送
$scope.details = [];
$scope.apply = function () {
angular.forEach($scope.screenMap, function (value, key) {
$scope.details.push({"ID": value.scrn_id,
"Read": value.Read,
"Write": value.Write});
});
console.log($scope.details);
}
答案 3 :(得分:1)
angular.forEach($scope.screenMap, function (value, key) {
$scope.details.push({
"ID": value.scrn_id,
"Read": value.Read,
"Write", value.Write
});
});
答案 4 :(得分:1)
你在一个循环中推动3次,一次两个物体。不要使用逗号来插入键值对(对象名:值)
javascript对象注释如下
var x = {“nameofhe Object1”:object value1,
“nameofhe Object2”:对象value2
}
3 * 2 * 3 = 18,这些是单个条目。
创建一个javascript对象并推入$ scope.details
$scope.details = [];
$scope.apply = function () {
angular.forEach($scope.screenMap, function (value, key) {
//$scope.details.push(value.scrn_id, value.Read, value.Write);
$scope.details.push({"ID": value.scrn_id,
"Read": value.Read,
"Write": value.Write});
});
console.log($scope.details);
}