0

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 ?

1 Answers1

1

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(' '));
Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34
  • Thanks! Is there a way to put a letter before every number ? tried putting it in like Array(len - 1 + 1).fill().map((item, index) => 1 + index).join("a"+' ') but then it will put the letter behind the number, and the last number wont get any letter, I want it like: "a1, a2, a3" – nanchalantflow May 30 '23 at 12:16
  • It can be just down with array.from....no fill, no map `Array.from({ length: 8 }, (_, i) => i + 1).join(' ')` – epascarello May 30 '23 at 12:32
  • 1
    `Array.from({ length: 8 }, (_, i) => 'a' + (i + 1)).join(' ')` – epascarello May 30 '23 at 12:48
  • :') <- when epascarello correct you – Simone Rossaini May 30 '23 at 13:05