1

My data looks like this...

[{"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}]

Here we can see 5 sets of data, and I need to be able to split them into 3 arrays.

The first array needs to have all the Max values.

22.625, 24.5, 24.125, 21.5, 85

The second needs to have all the Min values

18.625, 18.5, 18.625, 18.375, 18.375

And the last needs to have all the id's

1451433600000, 1451520000000, 1451606400000, 1451692800000, 1451779200000

Keeping them in the same order is important and if there is multiple duplicate values then I need to keep them.

Im getting the data with angular fire like this...

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 Answers 4

2

Use underscore and then its super easy:

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"]

You can also use native 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"]
Sign up to request clarification or add additional context in comments.

Comments

2

Here is a solution for your problem.

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);
}

Comments

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)
 });

Or using 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
});

Comments

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);
 });

This is also solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.