Search code examples
javascriptjqueryarrow-functionsmap-function

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?


Solution

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