0

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?

3
  • Can you tell us results's schema? Commented Apr 20, 2017 at 18:13
  • The documentation is pretty clear on how to use angular.forEach. Perhaps it's time to read it? Commented Apr 20, 2017 at 18:19
  • What are you trying to accomplish? Commented Apr 20, 2017 at 18:22

2 Answers 2

2

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);
   });
Sign up to request clarification or add additional context in comments.

4 Comments

The object that I want at the first element of the array is results. What are value and key coming from?
what do you have inside results? is it an array or a object?
I figured it out. I need the value to push. This worked. Thanks for your help despite the basic question.
@GloriaSantin Glad to help
0

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); })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.