JavaScript迭代多个嵌套集

时间:2016-02-10 10:23:50

标签: javascript json cordova for-loop

我正在构建一个PhoneGap" app" (发现很难解决什么是工作和操作)并且有一些动态JSON结果,我需要迭代几个不同的子集以显示易于理解的页面。

数据看起来像这样(例如)某些子集country1 / country2中可能有多个,单个或无;

{
months: [
  {
  index: 201602,
  year: "2016",
  mon: "February",
  country1: [
  {
    index: 12345678,
    l: [
      {
      name: "Test1",
      mname: "Test 1",
      net: "February 10, 2016 11:39:00 UTC",
      }
    ]
    },
    {
    index: 23456789,
    l: [
      {
      name: "Test2",
      mname: "Test 2",
      net: "February 10, 2016 11:39:00 UTC",
      }
    ]
    }
  ],
  country2: [ ]
},
{
index: 201603,
year: "2016",
mon: "March",
country1: [
{
    index: 546547657654,
    l: [
      {
      name: "Test1",
      mname: "Test 1",
      net: "March 10, 2016 11:39:00 UTC",
      }
    ]
}
],
country2: []
},
{
index: 201604,
year: "2016",
mon: "April",
country1: [ ],
country2: [
{
    index: 78676756,
    l: [
      {
      name: "Test1",
      mname: "Test 1",
      net: "April10, 2016 11:39:00 UTC",
      }
    ]
}
]
}
]
}

我能够将JSON.parse变成一个变量,当我在console.log中时我得到了

 { months:
   [ { index: 201602,
       year: '2016',
       mon: 'February',
       launches: [Object],
       tbds: [Object] },
     { index: 201603,
       year: '2016',
       mon: 'March',
       launches: [],
       tbds: [Object] },
     { index: 201604,
       year: '2016',
       mon: 'April',
       launches: [],
       tbds: [Object] } ] }

然后我使用此代码尝试获取值,但无论我尝试过什么(点形成,索引)我都无法获得要显示的值(我设法获得" undefined-undefined" at一点!

  for(var test in data.months){
    console.log(data.months[test]);
    var year = test.index[i].year;
    // var month = data.months.index.mon;
    s += "<p>" + year + "-" + month + "</p>";
  }
    $launchDiv.html(s);

任何人都愿意帮助我并解释如何处理这个问题?

谢谢!

2 个答案:

答案 0 :(得分:0)

使用基于索引的循环遍历JSON结构:

for (var i = 0; i < data.months.length; i++) {
  var year = data.months[i].year;
  var month = data.months[i].mon;
  s+= '<p>' + year + '-' + month;
}

$launchDiv.html(s);

答案 1 :(得分:0)

您可以使用Array.prototype.forEach()

<强>问题

var year = test.index[i].year;
// var month = data.months.index.mon;

首先,test是属性而不是obj。所以它应该是data.months[test]

其次,index是属性,而不是数组。因此index[i]undefinedindex[i].year也是data.months[test].year。你应该尝试var result = ""; obj.months.forEach(function(item) { result += "<p>" + item.year + "-" + item.mon + "</p>"; item.country1.forEach(function(c) { result += getObjectInString(c.l[0]); }); item.country2.forEach(function(c) { result += getObjectInString(c.l[0]); }); }); document.getElementById("content").innerHTML = result; function getObjectInString(obj) { var keys = Object.keys(obj); var result = "<ul>"; keys.forEach(function(k) { result += "<li>" + k + " : " + obj[k] + "</li>"; }); result += "</ul>" return result; }

逻辑

var obj = {
  months: [{
    index: 201602,
    year: "2016",
    mon: "February",
    country1: [{
      index: 12345678,
      l: [{
        name: "Test1",
        mname: "Test 1",
        net: "February 10, 2016 11:39:00 UTC",
      }]
    }, {
      index: 23456789,
      l: [{
        name: "Test2",
        mname: "Test 2",
        net: "February 10, 2016 11:39:00 UTC",
      }]
    }],
    country2: []
  }, {
    index: 201603,
    year: "2016",
    mon: "March",
    country1: [{
      index: 546547657654,
      l: [{
        name: "Test1",
        mname: "Test 1",
        net: "March 10, 2016 11:39:00 UTC",
      }]
    }],
    country2: []
  }, {
    index: 201604,
    year: "2016",
    mon: "April",
    country1: [],
    country2: [{
      index: 78676756,
      l: [{
        name: "Test1",
        mname: "Test 1",
        net: "April10, 2016 11:39:00 UTC",
      }]
    }]
  }]
}
var result = "";
obj.months.forEach(function(item) {
  result += "<p>" + item.year + "-" + item.mon + "</p>";
  item.country1.forEach(function(c) {
    result += getObjectInString(c.l[0]);
  });
  item.country2.forEach(function(c) {
    result += getObjectInString(c.l[0]);
  });
});
document.getElementById("content").innerHTML = result;

function getObjectInString(obj) {
  var keys = Object.keys(obj);
  var result = "<ul>";
  keys.forEach(function(k) {
    result += "<li>" + k + " : " + obj[k] + "</li>";
  });
  result += "</ul>"
  return result;
}

代码示例

&#13;
&#13;
<div id="content"></div>
&#13;
<view>
&#13;
&#13;
&#13;