jQuery: Loop through JSON array

Massimo Scola
Massimo Scola used Ask the Experts™
on
I would like to write a function that returns all orders and puts the OrderIDs in a JavaScript array.

The JSON looks like this:

{
    "status": "success",
    "data": [
        {
            "id": "69507",
            "client_id": "1",
            "date": "2019-05-18 11:02:20",
            "latitude": "0.00000000",
            "longitude": "0.00000000"
        },
        {
            "id": "69514",
            "client_id": "2",
            "date": "2019-05-18 11:15:20",
            "latitude": "0.00000000",
            "longitude": "0.00000000"
        },
        {
            "id": "69513",
            "client_id": "1",
            "date": "2019-05-18 11:15:16",
            "latitude": "0.00000000",
            "longitude": "0.00000000"
        },
        {
            "id": "69511",
            "client_id": "1",
            "date": "2019-05-18 11:13:23",
            "latitude": "0.00000000",
            "longitude": "0.00000000"
        },
        {
            "id": "69510",
            "client_id": "1",
            "date": "2019-05-18 11:12:01",
            "latitude": "0.00000000",
            "longitude": "0.00000000"
        },
        {
            "id": "71397",
            "client_id": "1",
            "date": "2019-05-20 22:33:36",
            "latitude": "41.00000000",
            "longitude": "0.00000000"
        }
    ]
}

Open in new window


As I'm using jQuery, I use the following method to loop through the array - and then get the "id":

var orderIDs = [];

function getAllOrderIDs() {
  var url = "http://137.108.92.9/openstack/api/orders?OUCU=" + oucu + "&password=" + password;
  /* Get the data */
  $.get(url, function (data) {
    var obj = $.parseJSON(data);
    if (obj.status == "success") {
      // was able to get the data successfully 
      // loop through all orders
      data = $.parseJSON(data);
        $.each(data, function(i, item) {
          //alert(item);
          orderIDs.push(data[item].id);
        });
    };
  })
}

Open in new window


I get the following error message:
index.js:49 Uncaught TypeError: Cannot read property 'id' of undefined

Line 49 is the line of code which I puts the orderID into the array.

Can someone please give me a clue what I'm doing wrong?

Thanks for your help

Massimo
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
replace :
orderIDs.push(data[ item ].id);
by :
orderIDs.push(data[ i ].id);
or by :
orderIDs.push(item.id);
leakim971Multitechnician
Top Expert 2014

Commented:
replace :
data = $.parseJSON(data);
by :
data = obj.data;
leakim971Multitechnician
Top Expert 2014

Commented:
var orderIDs = [];

function getAllOrderIDs() {
  var url = "http://137.108.92.9/openstack/api/orders?OUCU=" + oucu + "&password=" + password;
  /* Get the data */
  $.get(url, function (data) {
    var obj = $.parseJSON(data); // do you get JSON or a JSON string, if JSON, you should remove this
    if (obj.status == "success") {
      // was able to get the data successfully 
      // loop through all orders
        $.each(obj.data, function(i, item) {
          //alert(item);
          orderIDs.push(item.id);
        });
    };
  })
}

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Massimo ScolaSoftware Engineer

Author

Commented:
This seems to work

When I run
      console.log("ORDERS:");
      console.log(orderIDs);
      alert(orderIDs.length);

I get

Capture.PNG
Why is it, that orderIDs.length returns 0 ?
The array has been populated properly, hasn't it?
leakim971Multitechnician
Top Expert 2014

Commented:
what do you get in the alert box (not the console) ?
it say __proto___ is an empry array not the lenght of orderIDs
Massimo ScolaSoftware Engineer

Author

Commented:
I get 0 in the alert box.

But the array orderIDs is populated, isn't it?
I can see the OrderIDs and they are the same as the ones I get in Postman.
Multitechnician
Top Expert 2014
Commented:
got 6 : https://jsfiddle.net/mpgcn43w/

var obj = {
    "status": "success",
    "data": [
        {
            "id": "69507",
            "client_id": "1",
            "date": "2019-05-18 11:02:20",
            "latitude": "0.00000000",
            "longitude": "0.00000000"
        },
        {
            "id": "69514",
            "client_id": "2",
            "date": "2019-05-18 11:15:20",
            "latitude": "0.00000000",
            "longitude": "0.00000000"
        },
        {
            "id": "69513",
            "client_id": "1",
            "date": "2019-05-18 11:15:16",
            "latitude": "0.00000000",
            "longitude": "0.00000000"
        },
        {
            "id": "69511",
            "client_id": "1",
            "date": "2019-05-18 11:13:23",
            "latitude": "0.00000000",
            "longitude": "0.00000000"
        },
        {
            "id": "69510",
            "client_id": "1",
            "date": "2019-05-18 11:12:01",
            "latitude": "0.00000000",
            "longitude": "0.00000000"
        },
        {
            "id": "71397",
            "client_id": "1",
            "date": "2019-05-20 22:33:36",
            "latitude": "41.00000000",
            "longitude": "0.00000000"
        }
    ]
};
var orderIDs = [];
if (obj.status == "success") {
        $.each(obj.data, function(i, item) {
          //alert(item);
          orderIDs.push(item.id);
        });
      console.log("ORDERS:");
      console.log(orderIDs);
      alert(orderIDs.length);
};

Open in new window

Massimo ScolaSoftware Engineer

Author

Commented:
Oh I see.. the problem is the scope! As I'm creating a Cordova App, I put this function outside of the app{} part of the JavaScript.

Your code works, so let me fix the issue with the scope!

Thanks a lot for your help

Massimo
Massimo ScolaSoftware Engineer

Author

Commented:
Thank you for your time

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial