i am not understanding how can add commas after each class is copied i did it using for loop but it gives more different output than I want. There are around 9 div class of .name so when each one is copied i want add commas and remove extra space.
here is my code part:
const A = $('.tag-container.field-name').map((i, section) => {
let B = $(section).find('.name')
return B.text()
})
.get(2)
console.log(A)
There are two things you want to do here.
To remove any whitespace from the left or right-hand sides of a string (eg. from " foo "
to "foo"
), you can use the String.trim()
method.
Regarding the second point, I assume that in adding commas, you want to end up with a string of classnames, separated with commas, like "foo,bar,baz"
. The .map
method you are already using will return an array of something. You can join elements of an array together as a string with the Array.join()
method. The join
method takes a single argument which specifies the string to use in between each element.
Put those together, and you end up with something like:
const A = $(".tag-container.field-name")
.map((i, section) => {
let B = $(section).find(".name");
return B.text().trim(); // Note use of trim
})
.join(',') // Join all elements of the array with the `,` character
console.log(A);
// Something like `"foo,bar,baz"`