0

I need to create a multidimensional array using intervals. I have 5 users who's data grows every 5 seconds. Each user needs array to hold this data that can be accessed for each user later.

currently $rootScope.BHR looks like this:
[1,2,3,4,5,6,7,8,9,10...] <--after 10 secs/ 2 intervals

I want this <--after 10 sec/2 intervals
[1,6..] //key0
[2,7..] //key1
[3,8..] //key2 
[4,9..] //key3
[5,10..]//key5

//CODE

   var liveDataCtrl = angular.module("xo").controller("liveDataCtrl", ["$scope", "$rootScope", "$http", "$interval", "lodash",
        function ($scope, $rootScope, $http, $interval, lodash) {
            $rootScope.BHR = [];
            function shout() {
                $http.get("URL")
                        .then(function (live) {
                            $scope.live = live.data;
                            var key;
                            for (key in $scope.live) {
                                console.log($scope.live[key].personnelID, "keys");
                                var getId = $scope.live[key].personnelID;
                                $http.get(URL + getId)
                                        .then(function (all) {
                                            var data = all.data[0].HR;
                                   console.log(all.data[0].HR); 
                                            $rootScope.BHR.push(data);
                                        })
                            }
                        })
            }
            $interval(shout, 5000);

            function hrLive() {
                console.log($rootScope.BHR, "SHOUT");
            }
            $interval(hrLive, 5000);
        }]);
Jamal
  • 21
  • 9

1 Answers1

0

So, there are no true multi-dimensional arrays per se, what JavaScript has are Objects, which are more or less the same thing (but since labels matter, we'll call it what they are).

To declare a new object, use the syntax

var myObj = {};

Then, to append a new property you can use either dot or bracket notation:

var a = 'foo';
myObj[a] = [2,3,4];
myObj.bar = [5,6,7];
/**
 * myObj = {
 *   foo: [2, 3, 4],
 *   bar: [5, 6, 7]
 * };
 */

What you'll want to do is declare a new object when your code starts running, and then declare new attributes on that object as arrays. I created a plunk that demonstrates how to accomplish what I believe you are trying to do.

Caleb Williams
  • 1,035
  • 5
  • 12
  • Yes that is exactly what I am trying to do. Thanks! – Jamal Oct 23 '15 at 16:14
  • Sure thing, if the answer worked for you, please mark this question as answered so that in the future, anyone who has a similar question can come across the answer easily. – Caleb Williams Oct 23 '15 at 16:19
  • I will. would it be an issue using rootScope instead of scope? I want these objs to be global. – Jamal Oct 23 '15 at 16:27
  • You can use `$rootScope` the same way as `$scope`. I tend not to use the former because I would much rather use a service to cache data (and I use Angular's `controllerAs` syntax for data binding) within an application to keep my reliance on any `$rootScope/$scope` object as minimal as possible. My choice of `$scope` there was merely personal preference. The point to take away is that if you want functionality like a multidimensional array, you need to use an Object in JavaScript. – Caleb Williams Oct 23 '15 at 16:30