繁体   English   中英

遍历JS列表项

[英]Looping through JS list items

我正在提交一个api请求,该请求返回这样的响应。

    //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...

我将其视为数组,并尝试像这样循环遍历:

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

并且返回未定义。 我假设它是一个JSON列表,并且尝试使用console.log(JSON.stringify(list [i]))时没有运气。

编辑:这是我的整个通话,并且正在检索列表,因为api自动console.logs它。

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

做这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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