Dan Romulus Dan Romulus - 1 year ago 62
Javascript Question

Angularjs:How to fill a bidimensional array on the second position with data from $scope

I have a bidimensional array like filled like this:

biDimArr = new Array(6);
for(var i=0; i<6; i++){
biDimArr[i] = new Array(3);
biDimArr[i] = [i,0,0];

biDimArr = [[0, 0, 0], [1, 0, 0], [2, 0, 0], [3, 0, 0],[4, 0, 0],[5, 0, 0]]

If I iterate through a scope object like this:

var quantityArr=[];
angular.forEach($scope.sales, function (item){

I get this array :
quantityArr = [1, 2, 5, 5, 5, 3]

How can I insert the values from the quantityArr to the biDimArr on the second position of the inner arrays? Value "1" in the first array, val "2" in the second one,"5" in the third one and so on.

I need this array to populate a google line chart.

I know it must be a simple answer but I am not getting the right result.

Thank you. You help will be very appreciated.

Answer Source

You can use the following code:

quantityArr.forEach(function(quantity, index) { 
    biDimArr[index][1] = quantity;

Please note that it lacks any error handling, so quantityArr and biDimArr must have exactly the same number of elements.

Edit: To do it more efficiently you can also do:

$scope.sales.forEach(function(item, index) {
    // If you need the quantityArr populated:
    // Add the quantity to biDimArr:
    diDimArr[index][1] = item.quantity;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download