嗨,我有一组对象。请检查下面的代码。我有三个值'A','B','C'。例如,如果孟买不存在B值,我想按0。
// Code goes here
var c = angular.module('myApp', ['angular.filter'])
c.controller('myCtrl', function($scope, $filter) {
$scope.finalArray = [];
$scope.data = [{
"id": "1",
"place": "Mumbai",
"name": "A",
"value": "10"
}, {
"id": "4",
"place": "Mumbai",
"name": "B",
"value": "20"
}, {
"id": "4",
"place": "Delhi",
"name": "B",
"value": "77"
}, {
"id": "5",
"place": "Delhi",
"name": "C",
"value": "11"
}, {
"id": "6",
"place": "Banglore",
"name": "A",
"value": "14"
}, {
"id": "7",
"place": "Banglore",
"name": "C",
"value": "100"
},
{
"id": "3",
"place": "Delhi",
"name": "A",
"value": "30"
},]
$scope.finalArray = [];
$scope.stationName = [];
$scope.name = $filter('groupBy')($scope.data, 'name');
angular.forEach($scope.name, function(k, v) {
$scope.title = [];
$scope.count = [];
console.log(k, v);
$scope.title.push(v)
angular.forEach(k, function(key, value) {
$scope.count.push(key.value)
})
var obj = { name: $scope.title[0], data: $scope.count }
$scope.finalArray.push(obj);
});
console.log($scope.finalArray)
})
<!DOCTYPE html>
<html>
<head>
<script data-require="angular.js@1.7.0" data-semver="1.7.0" src="https://code.angularjs.org/1.7.0/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.17/angular-filter.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
{{finalArray | json}}
</body>
</html>
现在我得到的输出为
[ { "name": "A", "data": [ "10", "30", "14" ] }, { "name": "B", "data": [ "20", "77" ] }, { "name": "C", "data": [ "11", "100" ] } ]
But my desired output is [{name :"A",data :[10 30 14]},{name : "B",data : [20,77,10]},{name : "C",data : [0,11,100]}]
答案 0 :(得分:0)
我已经定义了一个distinct函数,该函数接受一个数组并返回数组中的distinct值。然后,我使用此函数获取名称和位置的明确列表。然后,我将名称列表映射到对象数组中,并为每个名称将地点列表映射到该地点的值中。
// Code goes here
var c = angular.module('myApp', ['angular.filter'])
c.controller('myCtrl', function($scope, $filter) {
$scope.finalArray = [];
$scope.data = [{
"id": "1",
"place": "Mumbai",
"name": "A",
"value": "10"
}, {
"id": "4",
"place": "Mumbai",
"name": "B",
"value": "20"
}, {
"id": "4",
"place": "Delhi",
"name": "B",
"value": "77"
}, {
"id": "5",
"place": "Delhi",
"name": "C",
"value": "11"
}, {
"id": "6",
"place": "Banglore",
"name": "A",
"value": "14"
}, {
"id": "7",
"place": "Banglore",
"name": "C",
"value": "100"
},
{
"id": "3",
"place": "Delhi",
"name": "A",
"value": "30"
},]
const distinct = (array) => array ? array.reduce((arr, item) => (arr.find(i => i === item) ? [...arr] : [...arr, item]), []) : array;
const names = distinct($scope.data.map(obj => obj.name));
const places = distinct($scope.data.map(obj => obj.place));
$scope.finalArray = names.map(name => {
return {
name: name,
data: places.map(place => {
const obj = $scope.data.find(o => o.place === place && o.name === name);
return obj ? obj.value : 0;
})
};
});
})
<!DOCTYPE html>
<html>
<head>
<script data-require="angular.js@1.7.0" data-semver="1.7.0" src="https://code.angularjs.org/1.7.0/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.17/angular-filter.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
{{finalArray | json}}
</body>
</html>