gpt4 book ai didi

Javascript如何获取嵌套在数组内的数组的长度,即在对象内?

转载 作者:行者123 更新时间:2023-11-29 23:41:14 25 4
gpt4 key购买 nike

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

这是我正在使用的代码:

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'
]
}
],

我可以用

定位名为“experience”的数组
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);
}

最佳答案

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

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);
}
}

关于Javascript如何获取嵌套在数组内的数组的长度,即在对象内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45187191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com