Angular - 将一个数组拆分为3个

时间:2016-01-03 13:00:01

标签: javascript arrays angularjs

我的数据看起来像这样......

[{"max":22.625,"min":18.625,"$id":"1451433600000","$priority":null},
{"max":24.5,"min":18.5,"$id":"1451520000000","$priority":null},
{"max":24.125,"min":18.625,"$id":"1451606400000","$priority":null},
{"max":21.5,"min":18.375,"$id":"1451692800000","$priority":null},
{"max":85,"min":18.375,"$id":"1451779200000","$priority":null}]

在这里我们可以看到5组数据,我需要能够将它们分成3个数组。

第一个数组需要具有所有Max值。

22.625, 24.5, 24.125, 21.5, 85

第二个需要拥有所有最小值

18.625, 18.5, 18.625, 18.375, 18.375

最后需要拥有所有ID

1451433600000, 1451520000000, 1451606400000, 1451692800000, 1451779200000

保持它们的顺序是相同的,如果有多个重复值,那么我需要保留它们。

我像这样用角火来获取数据......

var ref_day = new Firebase("https://mydb.firebaseio.com/1day");
$scope.day_log = $firebaseArray(ref_day);

angular.forEach($scope.day_log, function(value, key){

// I NEED SOME HELP HERE

});

4 个答案:

答案 0 :(得分:2)

以下是您的问题的解决方案。

var data = [
    {"max":22.625,"min":18.625,"$id":"1451433600000","$priority":null},
    {"max":24.5,"min":18.5,"$id":"1451520000000","$priority":null},
    {"max":24.125,"min":18.625,"$id":"1451606400000","$priority":null},
    {"max":21.5,"min":18.375,"$id":"1451692800000","$priority":null},
    {"max":85,"min":18.375,"$id":"1451779200000","$priority":null}
];

var minList = [];
var maxList = [];
var $idList = [];

for(var i=0; i<data.length; i++) {
    minList.push(data[i].min);
    maxList.push(data[i].max);
    $idList.push(data[i].$id);
}

答案 1 :(得分:2)

使用underscore,然后使用它非常简单:

myArray = [{"max":22.625,"min":18.625,"$id":"1451433600000","$priority":null},
    {"max":24.5,"min":18.5,"$id":"1451520000000","$priority":null},
    {"max":24.125,"min":18.625,"$id":"1451606400000","$priority":null},
    {"max":21.5,"min":18.375,"$id":"1451692800000","$priority":null},
    {"max":85,"min":18.375,"$id":"1451779200000","$priority":null}];

maxs = _.map(myArray, function(item) { return item.max }); // [22.625, 24.5, 24.125, 21.5, 85]
mins = _.map(myArray, function(item) { return item.min }); // [18.625, 18.5, 18.625, 18.375, 18.375]
ids = _.map(myArray, function(item) { return item.$id }); // ["1451433600000", "1451520000000", "1451606400000", "1451692800000", "1451779200000"]

您还可以使用原生Javascript:

maxs = myArray.map(function(item) { return item.max }); // [22.625, 24.5, 24.125, 21.5, 85]
mins = myArray.map(function(item) { return item.min }); // [18.625, 18.5, 18.625, 18.375, 18.375]
ids = myArray.map(function(item) { return item.$id }); // ["1451433600000", "1451520000000", "1451606400000", "1451692800000", "1451779200000"]

答案 2 :(得分:0)

 $scope.max = $scope.min = $scope.ids = [];

 angular.forEach($scope.day_log, function(v, key) {
   $scope.max.push(v.max);
   $scope.min.push(v.min);
   $scope.ids.push(v.$id)
 });

或使用Vanilla JavaScript

$scope.max = $scope.day_log.map(function(v, i) {
  return v.max
});
$scope.min = $scope.day_log.map(function(v, i) {
  return v.min
});
$scope.ids = $scope.day_log.map(function(v, i) {
  return v.$id
});

答案 3 :(得分:0)

var data = [
    {"max":22.625,"min":18.625,"$id":"1451433600000","$priority":null},
    {"max":24.5,"min":18.5,"$id":"1451520000000","$priority":null},
    {"max":24.125,"min":18.625,"$id":"1451606400000","$priority":null},
    {"max":21.5,"min":18.375,"$id":"1451692800000","$priority":null},
    {"max":85,"min":18.375,"$id":"1451779200000","$priority":null}
];
var minList = [];
var maxList = [];
var idList = [];

angular.forEach(data,function(value,key){
   minList.push(value.min);    
   maxList.push(value.max);
   idList.push(value.$id);
 });

这也是解决方案。