2

I am trying to get the length of an array that is inside an array, that is inside an object.

Here is the code I am working with:

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

This is what my data looks like:

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

I can target the array called "experience" with

resumeData.experience.length

However, when I try to target the "descriptions" array within the "experience" array with:

resumeData.experience.descriptions.length

the entire section disappears.

I tried using:

resumeData.experience[4].length

and that doesn't work.

Any help would be really appreciated!

Thanks!

** EDIT #2 **

After the recommendation, I adjusted the code to be as follows, but I only see

[object HTMLUListElement]

in place of the UL text that should be there. Here is what the code looks like now:

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);
}
5
  • resumeData.experience[0].descriptions.length - experience is an array, so you need to select an element first. Commented Jul 19, 2017 at 10:03
  • resumeData.experience[your index here].descriptions.length Commented Jul 19, 2017 at 10:04
  • This did not work. None of the entries show up after adding this. Commented Jul 19, 2017 at 10:16
  • what is the output on the console ? Commented Jul 19, 2017 at 15:03
  • with the edited code, I get: 2 2 8 4 3 3 8 8 9 9 In console Commented Jul 19, 2017 at 21:20

2 Answers 2

1

you were close, after get one element by index, you call the property by name, then ask the size:

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);
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

This did not work. After trying this, all variables disappear from the page. Here is what the code looks like after your recommendation: var experience = document.getElementById('experience'); for (j = 0; j < resumeData.experience[4].descriptions.length; j++){ '<li>'+resumeData.experience[i].descriptions[j]+'</li>'; entryDescriptions.appendChild(entryDescription); }`
@aarmfield I take a better look at my answer, and I edited it, take a look and let me know if that work
thanks for the clarification. I got this to work for the count using what you recommended (console prints correct number of entries) however, the descriptions are not showing up. I have updated my edit to have what I have found to be as close as I can get, but I only see [object HTMLUListElement] where the UL should be.
@aarmfield you almost got it, you have to only use the function appendChild correctly, please see this link stackoverflow.com/questions/38240544/… , your original question was answered. Now your new question is a little more tricky and I would need another kind of info, and it's al mixed, I think the best here is ask a new question, with the new issues. I'm glad to help :)
@aarmfield I mean, you have the data now correctly stored, you only need to display it well ;). Don't forget vote and accept the answer, I hope I can help with the new question!
|
0

i think you forgot to close the resumeData object with }

console.log(resumeData.experience[0].descriptions.length); //print 2

your code should be like :

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

1 Comment

Sorry, there is more to the data code than what I copy/pasted, so it is ended correctly, just much further down. Thanks for the heads up, though. I double checked it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.