EaBangalore EaBangalore - 2 months ago 8
Node.js Question

i am able to get the the text results how can i push them into array

I am able to get all the contact numbers,but result is coming in text format(

8045318762
,
9632427326
).

How can I store them into
array
like
["9632427326","8045318762"]
.
I mean similar to push.

My DOM is:

<span style="font-weight:bold;" class="number">
<span id="mob0">0
</span>8045318762
<script type="text/javascript"> whichVer('ver0'); </script>
</span>
<span style="font-weight:bold;" class="number">
<span id="mob1">1
</span>9632427326
<script type="text/javascript"> whichVer('ver1'); </script>
</span>

**I am using something like this:**


which returns only text i want array(similar to push).

alert($('.number').contents().filter(function() {
return this.nodeType == 3;
}).text());


Please help, thanks in advance.

Answer

You can use map() and get() and return array of numbers.

var data = $('.number').contents().map(function() {
  if (this.nodeType == 3 && this.textContent.trim() != '') return Number(this.textContent);
}).get();

console.log(data)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span style="font-weight:bold;" class="number">
  <span id="mob0">0
 </span>8045318762
</span>
<span style="font-weight:bold;" class="number">
  <span id="mob1">1
 </span>9632427326
</span>

Comments