简体   繁体   中英

Looping through JS list items

I am submitting an api request that returns a response like this.

    //Example Response
[
  {
"id": 34,
"user_id": 1,
"first_name": "Awesome",
"last_name": "Person",
"email": "awesome+person@paywhirl.com",
"phone": "4564564566",
"address": "123 Easy Street",
"city": "Los Angeles",
"state": "California",
"zip": "93003",
"country": "US",
"created_at": "2015-11-17 19:52:34",
"updated_at": "2015-11-21 04:29:45",
"gateway_reference": "783789y6r890",
"default_card": 34,
"gateway_type": "PayPal",
"gateway_id": 18,
"currency": "USD",
"deleted_at": null,
"utm_source": "",
"utm_medium": "",
"utm_term": "online payments",
"utm_content": "",
"utm_campaign": "",
"utm_group": "",
"metadata":""
  }, and so on...

I am treating this like an array and trying to loop through like this:

for(var i=0; i<list.length; i++) {
  console.log(list[i]);
}

and it's returning undefined. I'm assuming it's a JSON list, and I've tried using console.log(JSON.stringify(list[i])) with no luck.

EDIT: This is my entire call, and it is retrieving the list because the api automatically console.logs it.

paywhirl.Customers.getCustomers(null, function(err, pw_customers) {

// for each paywhirl customer...
for(var i=0; i<pw_customer.length; i++) {
    // get stripe customer with same gateway reference
    stripe.customers.retrieve(
      pw_customer[i].gateway_reference,
      function(err, stripe_customer) {
        // create user
        var user = new User({
            username: pw_customer[i].email,
            stripe_id: pw_customer[i].gateway_reference});
        count++;
      }
    );
}

console.log(pw_customers[0]);
})

Do this:

JSON.parse(list).forEach(function(obj, idx){
    console.log("Object " + idx, obj)
})

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