简体   繁体   中英

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

Output: 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){
    quantityArr.push(item.quantity);
});

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.

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:
    quantityArr.push(item.quantity);
    // Add the quantity to biDimArr:
    diDimArr[index][1] = item.quantity;
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM