0

I have an object (array type) ,its console representation looks like following image . please see the image

enter image description here

This array is created by restangulr using following code ,

restangularProvider.addResponseInterceptor(function (data, operation, what, url, response, deferred) {
                if (operation == "getList") {
                    var extractedData;
                    extractedData = data.result;
                    extractedData.paginginfo = data.paginginfo;
                    return extractedData;
                }
                if (operation != "get") {
                    var item = { status: response.status };
                    feedBackFactory.showFeedBack(item);
                }

                return response.data;
            });

How can I read the elements from this array, I want to extract properties like paginginfo ,also object collection

// The EDIT :1 js libraries I used here angularjsu 1.3.4, and restangular 1.4

My app.js : here I configured rest angular provider

restangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
    if (operation == "getList") {
        var extractedData;
        extractedData = data.result;
        extractedData.paginginfo = data.paginginfo;
        return extractedData;
    }
    if (operation != "get") {
        var item = {
            status: response.status
        };
        feedBackFactory.showFeedBack(item);
    }

    return response.data;
});

// according to my knowledge this function will intercept every ajax call (api calls) and modify the response , unfortunately I need to apply custom modification because the getlist method must return collection but my api returning object, so according to restangular ,the above code is the possible solution, and here its fine its fetching the data.

userservice.js : this is angular service which using restangular

function(restangular) {
    var resourceBase = restangular.all("account");

    this.getUsers = function(pagenumber, recordsize) {
        var resultArray = resourceBase.getList({
            page: pagenumber,
            size: recordsize
        }).$object;
    };

};

according to my knowledge .$object in restangulr resolve the promise and bring back the data, also I am getting the resultArray its looks like in the image in the console, here I can log this array so I think I got all the data from server and filled in this object. I applied some array accessing techniques available jquery and JavaScript like index base accessing , associate accessing but I am getting undefined ie.

 resultArray[1] //undifiend;
JLRishe
  • 99,490
  • 19
  • 131
  • 169
Binson Eldhose
  • 993
  • 3
  • 14
  • 35
  • How have you got it printed in the console? It implies you already have access to the array. – MasNotsram Dec 23 '14 at 11:19
  • possible duplicate of [How to return value from an asynchronous callback function?](http://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function) – JLRishe Dec 23 '14 at 11:22
  • @MasNotsram, its pritend but index based accessing is not working ,ie d[0] resulting undefined – Binson Eldhose Dec 23 '14 at 11:22
  • FYI, it's not generally good to assign new properties to an array. You should keep the paging info separate from the array. e.g. `{ items: data.result, pagingInfo: data.pagingInfo }` (or you could just use `data` without creating something new to contain its values. – JLRishe Dec 23 '14 at 11:23
  • Where do you expect the value to go after you have returned it? Your code is inside an asynchronous callback. I'm assuming you're having a problem in some code that you haven't shown us? Where is your code where you try to access the array? – JLRishe Dec 23 '14 at 11:24
  • @JLRishe, I agree but I am using restangular to access my resource and its is the recommend approach by them to read wrapped collection – Binson Eldhose Dec 23 '14 at 11:25
  • @JLRishe here is the code I am using to get, return resourceBase.getList({page:pagenumber,size:recordsize}).$object; – Binson Eldhose Dec 23 '14 at 11:27
  • I don't know why someone down voted the question, I think its not related to async reading , I am able to log the array, but I just want to extract its properties – Binson Eldhose Dec 23 '14 at 11:30
  • @BinsonEldhose Can you show us where you're trying to do that (in the context of your code)? I see that you pasted a small snippet here in the comments, but I don't know what that means. – JLRishe Dec 23 '14 at 11:31
  • @JLRishe https://github.com/mgonto/restangular/issues/981 – Binson Eldhose Dec 23 '14 at 11:35
  • @BinsonEldhose Please edit your question to show us. – JLRishe Dec 23 '14 at 11:36
  • @JLRishe question changed – Binson Eldhose Dec 23 '14 at 11:51

3 Answers3

1

In angular you can use angular.forEach(items, function(item){ //your code here});

Where items is the array you want to traverse.

If you want to access to one specific position use [], for example var item= items[5].

Then you can do item.property.


UPDATE

Your problem is that you are setting properties in an Array JS Object:

extractedData.paginginfo = data.paginginfo;

You should return the object data like it is and in your controller do something like:

var results= data.result;
var pagInfo= data.paginationInfo;

angular.forEach(results,function(result){});
Carlos Verdes
  • 3,037
  • 22
  • 20
0

It looks like the array is numerically indexed (0..1..5); you should be able to simply iterate through it using ForEach (in Angular) or .each (in Jquery).

Something like (JQuery):

$.each(array, function(key, value)
{
   // key would be the numerical index; value is the key:value pair of the array index's element.
   console.log(value.firstname); // should print the firstname of the first element.
});
XtraSimplicity
  • 5,704
  • 1
  • 28
  • 28
0

First of all, as I said in the comments, you shouldn't be attaching named properties to arrays. Return an object thact contains what you need:

if (operation == "getList") {
    return { values: data.result, paging: data.pagingInfo };
}

The getList() method returns a promise, so you need to use that:

this.getUsers = function(pagenumber, recordsize) {
    resourceBase.getList({
        page: pagenumber,
        size: recordsize
    }).then(function (data) {
        console.log(data.values[0]);
        console.log(data.paging.totalRecords);
    });
};
JLRishe
  • 99,490
  • 19
  • 131
  • 169