0

I have the following array with data:

[
{"count":1,"title":"Ark: Survival","platform":"Playstation PS4"},
{"count":2,"title":"Lara Croft:", "platform":"Playstation PS4"},
{"count":1,"title":"Madden NFL", "platform":"Playstation PS4"}
]

All in my angular Cart scope:

var data = $scope.cart;

now i would like to add to each row the order number, so i use a push:

$scope.cart.push({"orderNumber": $scope.OrderNumber});

the problem is that is adds the order number but as a separate row and not inside each of the rows. so my desired output should look like this:

[
{"count":1,"title":"Ark: Survival","platform":"Playstation PS4","orderNumber":1 },
{"count":2,"title":"Lara Croft:", "platform":"Playstation PS4","orderNumber":1},
{"count":1,"title":"Madden NFL", "platform":"Playstation PS4","orderNumber":1}
]

how would i push to each row of the array? i tried several versions but can't seem to get it to work.

3 Answers 3

1

in order to achieve that you should iterate through each object and set order number separately

angular.forEach($scope.cart, function (value, key) {
    value.orderNumber = $scope.OrderNumber;
}); 
Sign up to request clarification or add additional context in comments.

Comments

1

You need to iterate over each item and add the orderNumber as a property for each item. So you can use forEach function of the angular for that.

angular.forEach($scope.cart, (item) => item.orderNumber = $scope.OrderNumber)

Comments

0

just got an answer that i implemented but the user has deleted the response, however it works perfect, this was the solution that i used:

$scope.cart.forEach(item => item.orderNumber = $scope.OrderNumber)

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.