admin管理员组文章数量:1384149
I am using node.js/cheerio (the same as jQuery syntax) to parse the page. And when I am using:
$ = cheerio.load(body);
console.log($('.td-title a').text());
I have in my console very long string from words "mainaboutcontactusprofile" etc. How can I make an array of td text?
I am using node.js/cheerio (the same as jQuery syntax) to parse the page. And when I am using:
$ = cheerio.load(body);
console.log($('.td-title a').text());
I have in my console very long string from words "mainaboutcontactusprofile" etc. How can I make an array of td text?
Share Improve this question edited Sep 19, 2017 at 9:27 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Sep 18, 2017 at 23:27 Angry BAngry B 3052 silver badges18 bronze badges4 Answers
Reset to default 7None of the answers provided worked for me, but this did.
let arr = $('.td-title a').toArray().map((x) => { return $(x).text()});
Credit: Github
jQuery's map()
console.log($("td").map(function(){ return this.textContent}).get());
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
</tr>
</table>
I suggest using jQuery's $.map() function for this. It creates an array based on a matched set of elements:
var textArray = $('.td-title a').map( function(index, domElement) {
return domElement.innerText; //each element input gets turned into text output
});
console.log( textArray );
See http://api.jquery./map/ for more details
in javascript simply document.getElementsByTagName("td") return an array of all td.
Using jQuery you should use something like $('#table1 td')
版权声明:本文标题:javascript - Is there simple way to get array from td elements on page? (node.jscheeriojQuery) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744521791a2610498.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论