我能够得到文本结果如何将它们推入数组

时间:2021-03-20 21:46:37

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

我能够获得所有联系号码,但结果是以文本格式(8045318762,9632427326)。

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

如何将它们存储到像[“9632427326”,“8045318762”]这样的数组中。我的意思是类似于推。

My DOM is:

我的DOM是:

<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.

请提前帮助,谢谢。

2 个解决方案

#1


2  

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

您可以使用map()和get()并返回数字数组。

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>

#2


0  

You could use regex to filter out the mobile no text. like this :

您可以使用正则表达式过滤掉移动设备中没有文本。喜欢这个 :

  function getMobArr(){
    var arr=[];
    $.each($(".number"),function(index,value){
        //filter text with regex for mobile 10 digit no.
        var match=$(value).text().match(/([\d]{10})/i);
        arr.push(match[1]);
    })
    return arr;
}

output:

输出:

["8045318762", "9632427326"]

[“8045318762”,“9632427326”]

#1


2  

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

您可以使用map()和get()并返回数字数组。

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>

#2


0  

You could use regex to filter out the mobile no text. like this :

您可以使用正则表达式过滤掉移动设备中没有文本。喜欢这个 :

  function getMobArr(){
    var arr=[];
    $.each($(".number"),function(index,value){
        //filter text with regex for mobile 10 digit no.
        var match=$(value).text().match(/([\d]{10})/i);
        arr.push(match[1]);
    })
    return arr;
}

output:

输出:

["8045318762", "9632427326"]

[“8045318762”,“9632427326”]