I recently came across a scenario where I have an array of strings:
a= ['apple', 'rice','pasta','orange']
There is a button to randomly add items to this array and then save it in the database. For example:
a.push('carrot');
a.save();
I want to display this array in two separate divs on the DOM, with fruits like apple and orange on the left side and non-fruits like rice and pasta on the right side. The split doesn't need to be dynamic. Should I use filters or split the array into two variables and merge them back together later?
My current (messy) solution involves:
var a = getFromDb;
$scope.b = _.filter(a,isFruit);
$scope.c = _.filter(a,isNotFruit);
Fruits : <div ng-repeat=fruit in b> {{fruit}}</div>
Others : <div ng-repeat=notFruit in c> {{notFruit}}</div>
$scope.save = function() {
var a = $scope.b + $scope.c;
saveToDb(a);
}
However, I find this method quite ugly and would appreciate if someone could suggest a more elegant solution.