从map函数中检索数组

时间:2016-09-18 19:34:11

标签: javascript arrays

我正在抓取一系列单词,但我似乎无法将标签文本放入数组中。

这是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>

这个html我正在通过以下代码检索:

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

显示如下:

{ '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] } } }

期望的结果是这样的:

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

1 个答案:

答案 0 :(得分:0)

你得到的是jQuery对象,但你想要的是元素数组。使用.get()

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