简体   繁体   English

在嵌套JavaScript数组中循环播放问题

[英]Having Issues Looping Through Nested JavaScript Array

I am having issues looping through a nested JavaScript array and need some help. 我在遍历嵌套JavaScript数组时遇到问题,需要一些帮助。 I am trying to return a set of list items with both the property name and the value, but it's returning as undefined. 我正在尝试返回具有属性名称和值的一组列表项,但是它以未定义的形式返回。 Any help would be much appreciated as this is an important project. 任何帮助将不胜感激,因为这是一个重要的项目。

JavaScript 的JavaScript

$.getJSON("item-data.json", function(results) {
        $.each(results.CatalogEntryView, function(index, item) {
            console.dir(item.ItemDescription[0].features);

            document.getElementById("productHighlightsList").innerHTML = item.ItemDescription[0].features.forEach(enumerateProperties)

function enumerateProperties(key, val)
        {
            return "<li>" + key + val + "</li>"
        }

        });
    });

The console output from console.dir(item.ItemDescription[0].features); console.dir(item.ItemDescription[0].features);的控制台输出console.dir(item.ItemDescription[0].features); can be seen below and shows the nested data structure I am trying to access: 可以在下面看到,并显示了我尝试访问的嵌套数据结构:

在此处输入图片说明

Since you're using jQuery, you can just use each again for that nested array: 由于您使用的是jQuery,因此可以再次为该嵌套数组使用它们:

$.getJSON("item-data.json", function(results) {
    $.each(results.CatalogEntryView, function(index, item) {
        $.each(item.ItemDescription[0].features, function(k,v){
            $("#productHighlightsList").append("<li>" + k + v + "</li>");
        });
    });
});

So that we can test this, we must pretent we have your json: 为了能够对此进行测试,我们必须假装我们拥有您的json:

var results = {
    'CatalogEntryView': [
        {
            'ItemDescription': [
                {
                    'features': {
                        '0': '<strong>A</strong>',
                        '1': '<strong>B</strong>',
                        '2': '<strong>C</strong>',
                        '3': '<strong>D</strong>',
                        '4': '<strong>E</strong>',
                        '5': '<strong>F</strong>',
                        '6': '<strong>G</strong>'
                    }
                }
            ]
        }
    ]
};

$.each(results.CatalogEntryView, function(index, item) {
    $.each(item.ItemDescription[0].features, function(k,v){
        $("#productHighlightsList").append("<li>" + k + v + "</li>");
    });
});

Working test on CodePen: https://codepen.io/skunkbad/pen/OjoBNb 在CodePen上的工作测试: https ://codepen.io/skunkbad/pen/OjoBNb

Array.forEach function does not accumulate the array elements. Array.forEach函数不累积数组元素。 You could use Array.reduce function to append all list items, and Array.map to wrap <li> tags around each of the items: 您可以使用Array.reduce函数附加所有列表项,并使用Array.map<li>标签包装在每个项目周围:

$.getJSON("item-data.json", function(results) {
    $.each(results.CatalogEntryView, function(index, item) {
        console.dir(item.ItemDescription[0].features);

        document.getElementById("productHighlightsList").innerHTML = item.ItemDescription[0].features.map(function(item){
            return "<li>" + item + "</li>";
        }).reduce(function(accumulator , item) {
            return accumulator + item;
        });
    });
});

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

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