search

Home  >  Q&A  >  body text

javascript - json double-level nesting, how to traverse and concatenate strings?

var response = {

"msg": "success",
"code": 200,
"data": {
    "total": 328880,
    "ipInfo": [
        {
            "count": 3155,
            "key": "315.230.145.246",
            "subList": [
                {
                    "count": 3154,
                    "key": "8058230"
                },
                {
                    "count": 1,
                    "key": "713467"
                }
            ]
        },
        {
            "count": 3454645,
            "key": "215.230.145.246",
            "subList": [
                {
                    "count": 6154,
                    "key": "8058240"
                },
                {
                    "count": 1124,
                    "key": "1155530"
                }
            ]
        }                  
    ]
}

}
Like this, I need to loop through the concatenated strings and put them on the page. How to do it? ? ? I'm a newbie.

黄舟黄舟2744 days ago984

reply all(2)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-06-24 09:45:35

        var tempStr = '';
        response.data.ipInfo.forEach(function (item) {
            item.subList.forEach(function (subItem) {
                tempStr += "<p><span>" + subItem.count + "</span><span>" + subItem.key + "</span></p>";
            });
        });

    Vue:

    <tr v-for="item in ipInfo">
        <td v-for="subItem in item">{{ subItem.count }} - {{subItem.key }}</td>
    </tr>

    reply
    0
  • 巴扎黑

    巴扎黑2017-06-24 09:45:35

    It shouldn’t be difficult to provide ideas to the questioner without writing code, loop to determine the data type, and then recurse

    reply
    0
  • Cancelreply