简体   繁体   中英

How to extract data from array in javascript

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

在此处输入图片说明

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;

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

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

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

You could access your array properties like this:

http://jsfiddle.net/mmsmsj1r/

The code that is shown in the link:

var arrayTest = [{
        id: 1,
        name: "john",
        lastname: "appleseed"
    }, {
        id: 2,
        name: "john",
        lastname: "appleseed"
    }, {
        id: 3,
        name: "john",
        lastname: "appleseed"
    }];
    var a;

    for (a in arrayTest) {
        console.log(arrayTest[a].id + " " + arrayTest[a].name + "                                " + arrayTest[a].lastname);

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM