Javascript如何获取嵌套在Array中的Array的长度,即在Object内部?

时间:2017-07-19 10:00:48

标签: javascript arrays

我正在尝试获取数组内部的数组的长度,即在对象内部。

以下是我正在使用的代码:

var experience = document.getElementById('experience');

for (i = 0; i < resumeData.experience.length; i++){
    var experienceEntryDiv = document.createElement('div');
    experienceEntryDiv.className = "experienceEntryDiv";
    var entryTitle = '<h1>'+resumeData.experience[i].title+'</h1>';
    var entryOrganization = '<h2>'+resumeData.experience[i].organization+'</h2>';
    var entryYears = '<h1 class="text-right"><small>'+resumeData.experience[i].startYear+' - '+resumeData.experience[i].endYear+'</small></h1>';

    for (j = 0; j < resumeData.experience.descriptions.length; j++){
        var entryDescription = '<li>'+resumeData.experience[i].descriptions[j]+'</li>';
        entryDescriptions.appendChild(entryDescription);
    }
    var entryHTML = 
        '<div class="entry">'+
        '<div class="row">'+
            '<div class="col-md-9">'+entryTitle+entryOrganization+'</div>'+
            '<div class="col-md-3">'+entryYears+'</div>'+
        '</div>'+
        '<div class="row">'+entryDescriptions+'<hr /></div>'+
        '<hr /></div>'+
        '<br />';
    experienceEntryDiv.innerHTML = entryHTML;
    experience.appendChild(experienceEntryDiv);
    }

这就是我的数据:

    var resumeData = {

        experience: [
    {
        title: 'Title',
        organization: 'Company Name',
        startYear: 2017,
        endYear: 2017,
        descriptions: [
            'Using code stuff',
            'Used more code stuff'
        ]
    },
    {
        title: 'Title',
        organization: 'Company Name',
        startYear: 2017,
        endYear: 2017,
        descriptions: [
            'Using code stuff',
            'Used more code stuff'
        ]
    },
    {
        title: 'Title',
        organization: 'Company Name',
        startYear: 2017,
        endYear: 2017,
        descriptions: [
            'Using code stuff',
            'Used more code stuff'
        ]
    },
    {
        title: 'Title',
        organization: 'Company Name',
        startYear: 2017,
        endYear: 2017,
        descriptions: [
            'Using code stuff',
            'Used more code stuff'
        ]
    },
    {
        title: 'Title',
        organization: 'Company Name',
        startYear: 2017,
        endYear: 2017,
        descriptions: [
            'Using code stuff',
            'Used more code stuff'
        ]
    },
    {
        title: 'Title',
        organization: 'Company Name',
        startYear: 2017,
        endYear: 2017,
        descriptions: [
            'Using code stuff',
            'Used more code stuff'
        ]
    }
],

我可以使用

来定位名为“体验”的数组
resumeData.experience.length

但是,当我尝试使用:

定位“体验”数组中的“描述”数组时
resumeData.experience.descriptions.length

整个部分消失。

我尝试使用:

resumeData.experience[4].length

这不起作用。

任何帮助都会非常感激!

谢谢!

**编辑#2 **

在推荐之后,我将代码调整为如下,但我只看到

[object HTMLUListElement]

代替应该存在的UL文本。以下是代码的样子:

var experience = document.getElementById('experience');
var entryDescriptions = document.createElement('ul');


for (i = 0; i < resumeData.experience.length; i++){
    var experienceEntryDiv = document.createElement('div');
    experienceEntryDiv.className = "experienceEntryDiv";
    var entryTitle = '<h1>'+resumeData.experience[i].title+'</h1>';
    var entryOrganization = '<h2>'+resumeData.experience[i].organization+'</h2>';
    var entryYears = '<h1 class="text-right"><small>'+resumeData.experience[i].startYear+' - '+resumeData.experience[i].endYear+'</small></h1>';

    for (j = 0; j < resumeData.experience[i].descriptions.length; j++){
        descriptionCounts = resumeData.experience[i].descriptions.length;
        console.log(descriptionCounts);
        var entryDescItem = document.createElement('li');
        entryDescItem.className = "entryDescItem";
        var entrydesc = document.createTextNode(resumeData.experience[i].descriptions[j]);
        entryDescItem.appendChild(entrydesc);
        entryDescriptions.appendChild(entryDescItem);
    }
    var entryHTML = 
    '<div class="entry">'+
    '<div class="row">'+
        '<div class="col-md-9">'+entryTitle+entryOrganization+'</div>'+
        '<div class="col-md-3">'+entryYears+'</div>'+
    '</div>'+
    '<div class="row"><ul>'+entryDescriptions+'</ul><hr /></div>'+
    '</div>'+
    '<br />';
    experienceEntryDiv.innerHTML = entryHTML;
    experience.appendChild(experienceEntryDiv);
}

2 个答案:

答案 0 :(得分:1)

你很接近,在按索引获取一个元素后,你按名称调用属性,然后询问大小:

for (i = 0; i < resumeData.experience.length; i++) {
    var experienceEntryDiv = document.createElement('div');
    experienceEntryDiv.className = "experienceEntryDiv";
    var entryTitle = '<h1>' + resumeData.experience[i].title + '</h1>';
    var entryOrganization = '<h2>' + resumeData.experience[i].organization + '</h2>';
    var entryYears = '<h1 class="text-right"><small>' + resumeData.experience[i].startYear + ' - ' + resumeData.experience[i].endYear + '</small></h1>';

    for (j = 0; j < resumeData.experience[i].descriptions.length; j++) {

        var entryDescription = '<li>' + resumeData.experience[i].descriptions[j] + '</li>';
        entryDescriptions.appendChild(entryDescription);
    }
}

答案 1 :(得分:0)

我想您忘了用resumeData

关闭}对象
console.log(resumeData.experience[0].descriptions.length); //print 2

您的代码应该像:

    var resumeData = {

        experience: [
    {
        title: 'Title',
        organization: 'Company Name',
        startYear: 2017,
        endYear: 2017,
        descriptions: [
            'Using code stuff',
            'Used more code stuff'
        ]
    },
    {
        title: 'Title',
        organization: 'Company Name',
        startYear: 2017,
        endYear: 2017,
        descriptions: [
            'Using code stuff',
            'Used more code stuff'
        ]
    },
    {
        title: 'Title',
        organization: 'Company Name',
        startYear: 2017,
        endYear: 2017,
        descriptions: [
            'Using code stuff',
            'Used more code stuff'
        ]
    },
    {
        title: 'Title',
        organization: 'Company Name',
        startYear: 2017,
        endYear: 2017,
        descriptions: [
            'Using code stuff',
            'Used more code stuff'
        ]
    },
    {
        title: 'Title',
        organization: 'Company Name',
        startYear: 2017,
        endYear: 2017,
        descriptions: [
            'Using code stuff',
            'Used more code stuff'
        ]
    },
    {
        title: 'Title',
        organization: 'Company Name',
        startYear: 2017,
        endYear: 2017,
        descriptions: [
            'Using code stuff',
            'Used more code stuff'
        ]
    }
]}