Reputation: 2136
I am using ag-grid for an application and it only takes arrays to display the rows. I have an object returned from the API. This call always only returns 1 object. I want to convert the object into an array of length 1. This is my code:
jobResult.paged().$promise.then(function (results) {
//ag-grid needs the result to be in an array
var arrayResult = [];
angular.forEach(function(results){
arrayResult.push(results);
});
$scope.jobResult = arrayResult;
if ($scope.lastResultGridOptions.api) {
$scope.lastResultGridOptions.api.setRowData($scope.jobResult);
$scope.lastResultGridOptions.api.sizeColumnsToFit();
}
..rest of function
the results
object has the data from the API. But the .foreach
function does not push the object into the array.
What am I missing?
Upvotes: 0
Views: 43
Reputation: 222582
Your angular.foreach
is wrong,
The correct way is, and then you can take the key or value and push to the array,
angular.forEach(results, function(value, key){
//push key or value
arrayResult.push(value);
});
Upvotes: 2
Reputation: 466
For your explanation, wanting an array length 1 with the results as [0], why not just push the results into the array:
arrayResult.push(results)
If you are trying to create an array from the results then you would want to run the .forEach on the results:
results.forEach(function(res) { arrayResult.push(res); })
Upvotes: 0