Home > Software engineering >  How to store text of tag "a" of a div in an arrray
How to store text of tag "a" of a div in an arrray

Time:12-25

I'm working on API, which extracts data from a web site. and the website is like - >

<div id="mainContainer">
<a >text1</a>
<a >text2</a>
<a >text3</a>
<a >text4</a>
<a >text5</a>
</div>

I want to store all text in an object like {"item1":"text1","item2":"text2"......}; here is what I'm doing

var prediction = $('#mainContainer > item');
console.log(prediction);

output

<a >text1</a><a >text2</a><a >text3</a>....

How do I do it ??

CodePudding user response:

You can create an object and then populate it with an each

values = {};
$('#mainContainer a.item').each((i,x) => values['item' (i 1)] = $(x).text())
  • Related