Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrow function within jQuery map

I'm trying to use an arrow function within jQuery's map function. With the following titlesText is the correct length but each string is empty:

let titles = $(panelBody).find('h4');
let titlesText = $(titles).map(title => $(title).text());

My ES6 transpiling is working, and jQuery is working. Any ideas?

like image 615
Ted Fitzpatrick Avatar asked Aug 27 '26 08:08

Ted Fitzpatrick


1 Answers

http://api.jquery.com/map/

The first argument to jQuery map is the index.

let testTitlesText = $(testTitles).map((index, testTitle) => $(testTitle).text());

Also as just a side note, you could use testTitle.innerText in the map to avoid creating a new jQuery object for each map invocation.

//testTitles is already a jQuery object, and you can use innerText
let testTitlesText = testTitles.map((index, testTitle) => testTitle.innerText);
like image 114
Taplar Avatar answered Aug 28 '26 23:08

Taplar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!