Search code examples
javascriptjquerynumbers

Count length of XML Object


With my JQuery call I can get the length of my Elements like this:

$(xml).find('PARENT').find('CHILDREN').length

from that I will get how many times my Element is exisiting. My current State looks like this.

Currently:

$(xml).find('PARENT').find('CHILDREN').length

Output> 8

What Output I want:

Output> 1 2 3 4 5 6 7 8

How can I count my length up ?


Solution

  • As i said in the comment you can use this fucntion

    const len = 8;
    //const len = $(xml).find('PARENT').find('CHILDREN').length;
    
    console.log(Array(len).fill().map((_, index) => 1 + index).join(' '));

    This function will take Max length (8 in this case) and fill an array with map, then use join for create a string from the array itself.

    Edit after comment:

    const len = 8;
    //const len = $(xml).find('PARENT').find('CHILDREN').length;
    
    console.log(Array.from({ length: 8 }, (_, i) => 'a' + (i + 1)).join(' '));