Scroll down for solution !!
I have a simple AngularJS application (with lodash loaded after angular.js, before app.js) :
- Project list page(/projects), where a link on an item brings it to /projects/:id
- In /projects/:id, the controller does a get request to an API, and returns the data
- I assign the data to $scope.project, and display it in the view
Here's an example of the return I'm getting from the API :
{
"_id" : { "$oid" : "546a3bdee4b0bfd97138fe08"} ,
"picture" : "http://placehold.it/400x400" ,
"name" : "Square Campus" ,
"address" : "293 Grafton Street, Shasta, Arizona, 6757" ,
"phone" : "+1 (916) 544-2274" ,
"buildings" : [
{ "name" : "North Campus" , "floors" : "4" , "users" : "8"} ,
{ "name" : "South Campus" , "floors" : "2" , "users" : "15"} ,
{ "name" : "East Wing" , "floors" : "8" , "users" : "23"}
]
}
// Using this call in the controller
$scope.project = Restangular.one('projects', $routeParams.projectId).get().$object;
And here's how I display it in the view :
<aside class="aside-primary col-xs-12 col-md-3">
<div class="well">
<img ng-src="{{project.picture}}" alt="" class="img-responsive"/>
<strong>{{project.name}}</strong><br />
{{project.address}}<br />
{{project.phone}}<br />
</div>
</aside>
<section class="primary-content col-xs-12 col-md-9">
<h1>{{project.name}}</h1>
<h2>Buildings</h2>
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Floors</th>
<th>Users</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="building in project.buildings">
<td>{{building.name}}</td>
<td>{{building.floors}}</td>
<td>{{building.users}}</td>
</tr>
<tr>
<td><strong>Total</strong></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</section>
Now, here's what I want to do :
I'm fairly new to Lo-Dash (and JS development is not my strongest asset) and would like to use it to calculate sums. In this case, the sum of the total number of floors and users.
Problem is, I don't know how to convert the object I'm receiving to this kind of array :
// Floors example
var sum = _.reduce([4, 2, 8], function(sum, num) {
return sum + num;
});
console.log(sum);
Can anyone help me figure this out?
Thank you !
Solution
It is important that when you make the Restangular call, you define the "then" promise because the data isn't loaded at first on the initial pageload.
var detailProject = Restangular.one('projects', $routeParams.projectId);
detailProject.get().then(function (project) {
$scope.project = project; // Assign the owner to a model
var sums = _.reduce(project.buildings,
function (sums, building) {
return {
floors: sums.floors + parseInt(building.floors),
users: sums.users + parseInt(building.users)
};
},
{ floors: 0, users: 0 } // initial values
);
console.log(sums);
});
lodashhere? @Jordan' solution is good but on every iteration a new object is created. It's not so good from performance point of view. I would recommend to use simpleforloop for such trivial task.sumsobject and return it instead of creating a new object each time (like this).