My goal is to populate a bootstrap-carousel using a more detailed JSON file pulled from a database. To illustrate, here is an example of my old JSON structure:
old.json
[
{"screen": [{
"img" : "../static/images/product/34.jpg",
"price": "Rs 100",
"href": "#mobile/1234"
},{
"img": "../static/images/product/34.jpg",
"price": "Rs 101",
"href":"#mobile/1234"
},...
]},
{"screen": [{
"img" : "../static/images/product/34.jpg",
"price": "Rs 100",
"href": "#mobile/1234"
},{
"img": "../static/images/product/34.jpg",
"price": "Rs 101",
"href":"#mobile/1234"
},...
]}
]
Now, I am aiming to update it with a new.JSON that provides more detailed information.
new.JSON
[
{
"sku": "d58a2ece-4387-41d0-bacb-c4b7935f8405",
"selectedQtyOptions": [],
...
},
{
"sku": "b8ea355d-7acc-455b-a55c-027b0e5c73cd",
"selectedQtyOptions": [],
...
},
...
]
To achieve this, I used an AngularJS controller and a loop to group the data in sets of four under the screen attribute:
mainApp.controller('MultiCarouselController', function($scope, $http) {
$scope.products = [];
$scope.Math = Math;
$http.get("/get_broad_category_products/?BroadCategory=BroadCategory3")
.success(function (response) {
$scope.products = response;
var iteration = Math.ceil($scope.products.length/4);
var carouselJson = [];
for(var index = 0; index < iteration; index++){
var temp = [];
for (var i = (index*4) ; i < (4*(index+1)) ; i++){
temp = temp.concat($scope.products[i])
}
var jsTemp = { "screen": [] };
carouselJson.push(jsTemp.screen.push(temp));
}
console.log(carouselJson);
}).error(function(){
console.log('An error occurred.');
});
});
This code successfully groups the data into sets of four under the screen property. Hopefully, this explanation helps clarify the process!