获取推送不是填充数组的函数

时间:2018-05-04 06:38:26

标签: javascript jquery arrays

我有这个函数来填充数组:

var arrEmailID = [];

var emailSpan = $(_PeoplePicker).find('.sp-peoplepicker-userSpan');
var _sid = "";
if (emailSpan.length > 0) {
    $(emailSpan).each(function() {
        _sid = $(this).attr('sid');
        _sid = _sid.split("|")[2];

        arrEmailID.push("'" + _sid + "'");
        arrEmailID = arrEmailID.join(",")
    })

} else {
    alert("null");
}

第一个值是推动成功的照片:

enter image description here

但是当它试图推动第二个时它会返回:

  

未捕获类型错误:.push不是函数

5 个答案:

答案 0 :(得分:2)

在循环外使用arrEmailID = arrEmailID.join(",")

var arrEmailID = [];
var emailSpan = $(_PeoplePicker).find('.sp-peoplepicker-userSpan');
var _sid = "";
if (emailSpan.length > 0) {
    $(emailSpan).each(function (){
        _sid = $(this).attr('sid');
        _sid =  _sid.split("|")[2];
        arrEmailID.push("'" + _sid + "'");      
    });
    arrEmailID = arrEmailID.join(",");
} else {
    alert("null");
}

答案 1 :(得分:1)

由于代码中的以下行

,这种情况正在发生
arrEmailID = arrEmailID.join(",")

上面一行会返回一个字符串,并将此行arrEmailID发布为string,而不是array

供参考,Array.join

答案 2 :(得分:0)

Array.prototype.join()将返回String
因此arrEmailID = arrEmailID.join(",")之后,typeof arrEmailID == "string"
对于String没有.push()。所以你发现了一个错误 解决方案在@Anfath Hifans的回答中提到:
循环后执行arrEmailID = arrEmailID.join(",");

答案 3 :(得分:0)

您可以更简单地编写代码:

var arrEmailID = Array.from($(_PeoplePicker).find('.sp-peoplepicker-userSpan'))
.map(span=>(span.getAttribute('sid')||"||"))
//arrEmailID should be an array
console.log(arrEmailID);
//if you want a string you can do:
console.log(arrEmailID.join(","));

答案 4 :(得分:0)

当您使用 jQuery.each() 时,您已将arrEmailID加入,并将其分配到该数组内的该数组中。所以不要把数组放在数组外面。

<强>样本

&#13;
&#13;
var emailSpan = $('#_PeoplePicker').find('.sp-peoplepicker-userSpan'),
  arrEmailID = [];

$(emailSpan).each((index,el) => {
 arrEmailID.push(`'${$(el).attr('sid').split("|")[2]}'`);
});

console.log(arrEmailID);

console.log(arrEmailID.join(','));
&#13;
.as-console-wrapper {max-height: 100% !important;top: 0;}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="_PeoplePicker">
  <span sid="a|B|abc@abc.com" class="sp-peoplepicker-userSpan"></span>
  <span sid="a|B|test@abc.com" class="sp-peoplepicker-userSpan"></span>
  <span sid="a|B|ta@abc.com" class="sp-peoplepicker-userSpan"></span>
  <span sid="a|B|tessss@abc.com" class="sp-peoplepicker-userSpan"></span>
</div>
&#13;
&#13;
&#13;

您还可以使用 jQuery.map() ES6的组合来获得所需的结果。

  

map()方法创建一个新数组,其结果是在调用数组中的每个元素上调用提供的函数。

&#13;
&#13;
var emailSpan = $('#_PeoplePicker').find('.sp-peoplepicker-userSpan');

let result = $.map(emailSpan, el => `'${$(el).attr('sid').split("|")[2]}'`);

console.log(result);

console.log(result.join(','));
&#13;
.as-console-wrapper {max-height: 100% !important;top: 0;}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="_PeoplePicker">
  <span sid="a|B|abc@abc.com" class="sp-peoplepicker-userSpan"></span>
  <span sid="a|B|test@abc.com" class="sp-peoplepicker-userSpan"></span>
  <span sid="a|B|ta@abc.com" class="sp-peoplepicker-userSpan"></span>
  <span sid="a|B|tessss@abc.com" class="sp-peoplepicker-userSpan"></span>
</div>
&#13;
&#13;
&#13;