Peter Pik Peter Pik - 2 months ago 22
Javascript Question

Retrieve array from map function

i'm scraping an array of words, but i can't seem to get the a tags text into an array.

this is the html

<span class="entry-categories">
Filed Under:
<a href="http://" rel="category tag">Review</a>,
<a href="http://" rel="category tag">News</a>,
<a href="http://" rel="category tag">Sport</a>,
<a href="http://" rel="category tag">Cars</a>
<a href="http://" rel="category tag">Events</a>
<a href="http://" rel="category tag">Constest</a>
<a href="http://" rel="category tag">Casino</a>
</span>


this html i'm retrieving by following code:

var keyWords = $('span.entry-categories a').map(function(index, element) {
return $(this).text();
});


it is showing like this:

{ '0': 'Review',
'1': 'News',
'2': 'Sport',
'3': 'Cars',
'4': 'Events',
'5': 'Constest',
'6': 'Casino',
options:
{ withDomLvl1: true,
normalizeWhitespace: false,
xmlMode: false,
decodeEntities: true },
_root:
{ '0':
{ type: 'root',
name: 'root',
attribs: {},
children: [Object],
next: null,
prev: null,
parent: null },
options:
{ withDomLvl1: true,
normalizeWhitespace: false,
xmlMode: false,
decodeEntities: true },
length: 1,
_root: [Circular] },
length: 7,
prevObject:
{ '0':
{ type: 'tag',
name: 'a',
attribs: [Object],
children: [Object],
next: [Object],
prev: [Object],
parent: [Object] },
'1':
{ type: 'tag',
name: 'a',
attribs: [Object],
children: [Object],
next: [Object],
prev: [Object],
parent: [Object] },
'2':
{ type: 'tag',
name: 'a',
attribs: [Object],
children: [Object],
next: [Object],
prev: [Object],
parent: [Object] },
'3':
{ type: 'tag',
name: 'a',
attribs: [Object],
children: [Object],
next: [Object],
prev: [Object],
parent: [Object] },
'4':
{ type: 'tag',
name: 'a',
attribs: [Object],
children: [Object],
next: [Object],
prev: [Object],
parent: [Object] },
'5':
{ type: 'tag',
name: 'a',
attribs: [Object],
children: [Object],
next: [Object],
prev: [Object],
parent: [Object] },
'6':
{ type: 'tag',
name: 'a',
attribs: [Object],
children: [Object],
next: null,
prev: [Object],
parent: [Object] },
options:
{ withDomLvl1: true,
normalizeWhitespace: false,
xmlMode: false,
decodeEntities: true },
_root: { '0': [Object], options: [Object], length: 1, _root: [Circular] },
length: 7,
prevObject: { '0': [Object], options: [Object], length: 1, _root: [Circular] } } }


the desired result is something like this:

['Review', 'News', 'Sport', 'Cars', 'Events', 'Constest', 'Casino']

Answer

What you got is the jQuery object, but you wanted the array of elements. Use .get():

var keyWords = $('span.entry-categories a').map(function(index, element) {
  return $(this).text();
}).get();