After retrieving an object from my REST API
, I need to iterate over the array and create new objects/arrays from it.
The structure of the array is as follows:
orderItem
contains menu_modifier_groups
, which in turn contain menu_modifier_items
.
The hierarchy is: orderItem
-> menu_modifier_groups
-> menu_modifier_items
orderItem: {
id: 159
name: Empanadas (Choice of 2)
description: Choice of Diced Beef; Spinach, Stilton and Onion; or Smoked Ham and Mozzarella
price: 700
available: 1
created_at: 2016-01-31 16:50:31
updated_at: 2016-01-31 16:50:31
menu_category_id: 41
restaurant_id: 11
menu_modifier_groups:
[{
id: 9
name: Choose 2 Empanadas
instruction: null
min_selection_points: 2
max_selection_points: 2
force_selection: 1
created_at: '2016-02-01 01:03:35'
updated_at: '2016-02-01 01:12:23'
menu_item_id: 159
restaurant_id: 11
menu_modifier_items:
[{
id: 34
name: Diced Beef
price: 0
created_at: '2016-02-01 01:04:08'
updated_at: '2016-02-01 01:04:08'
menu_modifier_group_id: 9
restaurant_id: 11
menu_item_id: 159
selected: true
}, {
id: 35
name: Smoked Salmon & Mozzarella
price: 0
created_at: '2016-02-01 01:04:39'
updated_at: '2016-02-01 01:04:37'
menu_modifier_group_id: 9
restaurant_id: 11
menu_item_id: 159
selected: true
}]
}]
}
Upon receiving this data from the REST API
, my goal is to transform the orderItem
into:
// main item + menu_modifier_items(where selected = true) without menu_modifier_groups
$scope.mainItem = {
id: 159
name: Empanadas (Choice of 2)
description: Choice of Diced Beef; Spinach, Stilton and Onion; or Smoked Ham and Mozzarella
price: 700
available: 1
created_at: '2016-01-31 16:50:31'
updated_at: '2016-01-31 16:50:31'
menu_category_id: 41
restaurant_id: 11
menu_modifier_items: [{
id: 34
name: Diced Beef
price: 0
created_at: '2016-02-01 01:04:08'
updated_at: '2016-02-01 01:04:08'
menu_modifier_group_id: 9
restaurant_id: 11
menu_item_id: 159
selected: true
},{
id: 35
name: Smoked Salmon & Mozzarella
price: 0
created_at: '2016-02-01 01:04:39'
updated_at: '2016-02-01 01:04:37'
menu_modifier_group_id: 9
restaurant_id: 11
menu_item_id: 159
selected: true
}]
}
This transformation allows me to work with $http
as shown below:
$scope.addItem = function(orderItem) {
//transform orderItem here into mainItem
$http({
method: 'post',
url: "http://api.example.com/web/cart/item/add",
data: $.param({
'cartItem': $scope.mainItem
}),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function (data, status, headers, config) {
});
}
Your advice and guidance on this process would be highly appreciated.