【问题标题】:How do I iterate through object array and have the results appear in a list?如何遍历对象数组并将结果显示在列表中?
【发布时间】:2019-06-07 02:37:00
【问题描述】:

我正在尝试从一组联系人中创建一个无序列表。

数组类似于:

let contacts = [{firstName:"John", lastName:"Doe", age:46}, {firstName:"Mike", lastName:"Smith", age:46}, {firstName:"Joe", lastName:"Dirt", age:46}];

我可以像这样从一个基本数组创建一个列表:

var options = [
        set0 = ['Option 1','Option 2'],
    ];

function makeUL(array) {
    // Create the list element:
    var list = document.createElement('ul');

    for(var i = 0; i < array.length; i++) {
        // Create the list item:
        var item = document.createElement('li');

        // Set its contents:
        item.appendChild(document.createTextNode(array[i]));

        // Add it to the list:
        list.appendChild(item);
    }

    // Finally, return the constructed list:
    return list;
}

// Add the contents of options[0] to #foo:
document.getElementById('foo').appendChild(makeUL(options[0]));

我不太明白如何使用对象制作列表。

【问题讨论】:

  • ` item.appendChild(document.createTextNode(array[i].firstName));`
  • 在makeUL(contacts) 的情况下,您的array[i] 将引用对象。例如,您可以通过 array[i].firstName 或 array[i].lastName 访问该对象的各个属性值。

标签: javascript arrays list


【解决方案1】:

在 JavaScript 中,对象可以使用 .或 []。

例如)array[i].firstName 或 array[i]['firstName']

您可以使用模板字符串或 JSON.stringify

const item = array[i];

item.appendChild(document.createTextNode(`firstName: ${item.firstName}, lastName: ${item.lastName}`));

或

item.appendChild(document.createTextNode(JSON.stringify(array[i]));

【讨论】:

    【解决方案2】:
    let contacts = [
        {firstName:"John", lastName:"Doe", age:46}, 
        {firstName:"Mike", lastName:"Smith", age:46}, 
        {firstName:"Joe", lastName:"Dirt", age:46}
    ];
    
    function makeUL(array) {
        // Create the list element:
        var list = document.createElement('ul');
    
        for(var i = 0; i < array.length; i++) {
            // Create the list item:
            var item = document.createElement('li');
    
            // Set its contents:
            item.appendChild(document.createTextNode(array[i].firstName + ' ' + array[i].lastName + ' ' + array[i].age));
    
            // Add it to the list:
            list.appendChild(item);
        }
    
        // Finally, return the constructed list:
        return list;
    
    }
    
    document.getElementById('foo').appendChild(makeUL(contacts));
    
    

    按照 tyler 的建议,我将在解决方案中添加一些上下文。我复制了相同的功能,然后就使用了 .访问对象的内容。

    【讨论】:

    • 考虑提供一些上下文、解释等。仅代码的“发现差异”答案很少是好的答案。
    【解决方案3】:

    我刚刚在浏览器控制台中对此进行了测试。不知道你在找什么,但如果你有一个对象数组并想从中创建一个 ul 列表,你可以尝试这样的事情:

    var options = [{firstName:"John", lastName:"Doe", age:46}, {firstName:"Mike", lastName:"Smith", age:46}, {firstName:"Joe",姓氏:“污垢”,年龄:46}];

    function makeUL(array) {
        // Create the list element:
        var list = document.createElement('ul');
    
        for(var i = 0; i < array.length; i++) {
            // Create the list item:
            var item = document.createElement('li');
            for (var key in array[i])
            // Set its contents:
            item.appendChild(document.createTextNode(array[i][key] + " "));
    
            // Add it to the list:
            list.appendChild(item);
        }
    
        // Finally, return the constructed list:
        return list;
    }
    
    // Add the contents of options[0] to #foo:
    
    alert(makeUL(options).outerHTML);
    

    这会给你这样的东西:

    <ul><li>John Doe 46 </li><li>Mike Smith 46 </li><li>Joe Dirt 46 </li></ul>
    

    你可以修改它以满足你的需要。

    这是另一个变体:

    var options = [{firstName:"John", lastName:"Doe", age:46}, {firstName:"Mike", lastName:"Smith", age:46}, {firstName:"Joe", lastName:"Dirt", age:46}];
    
    function makeUL(array) {
        // Create the list element:
        var list = document.createElement('ul');
    
        for(var i = 0; i < array.length; i++) {
            // Create the list item:
            var item = document.createElement('li');
            var text = new Array();
            for (var key in array[i]) {
            // Set its contents:
            text.push(array[i][key]);
            }
            item.appendChild(document.createTextNode(text.join(",")));
            // Add it to the list:
            list.appendChild(item);
        }
    
        // Finally, return the constructed list:
        return list;
    }
    
    // Add the contents of options[0] to #foo:
    
    alert(makeUL(options).outerHTML);
    

    【讨论】:

    • 我正在重新考虑我的方法...请在此处查看代码笔。 codepen.io/aspiringpro/pen/BegJyB我想找到一种方法将联系人数据放入适当的 div 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 2016-08-29
    • 2016-02-23
    • 1970-01-01
    相关资源
    最近更新 更多