Javascript 使用indexOf()组合唯一值数组
我正在遍历一个XML。子元素之一是Javascript 使用indexOf()组合唯一值数组,javascript,jquery,indexof,Javascript,Jquery,Indexof,我正在遍历一个XML。子元素之一是GroupName,它在XML字符串/文件中不是唯一的。因此,如果您愿意的话,我试图将这些GroupName标记的innertext的不同(唯一)匹配项放在一个数组中 <GroupName></GroupName> <GroupName></GroupName> <GroupName>VIII</GroupName> <GroupName>Date and signature&l
GroupName
,它在XML字符串/文件中不是唯一的。因此,如果您愿意的话,我试图将这些GroupName
标记的innertext的不同(唯一)匹配项放在一个数组中
<GroupName></GroupName>
<GroupName></GroupName>
<GroupName>VIII</GroupName>
<GroupName>Date and signature</GroupName>
<GroupName>Date and signature</GroupName>
<GroupName>Date and signature</GroupName>
<GroupName>Date and signature</GroupName>
<GroupName>VII</GroupName>
然而,当我尝试使用indexOf()
组成一个唯一值数组时,其中一个值似乎被跳过了
$xmlDoc.find('GroupName').each(function () {
$groupName = ($(this).text().length == 0) ? 'empty' : $(this).text();
if (groupnames.indexOf($groupName) < 0) {
groupnames.push($groupName);
}
});
$xmlDoc.find('GroupName')。每个(函数(){
$groupName=($(this.text().length==0)?“空”:$(this.text();
if(groupnames.indexOf($groupName)<0){
groupName.push($groupName);
}
});
在这种情况下,我得到“空”、“VIII”和“日期和签名”VII'不知怎么丢失了,我不知道为什么。你能发布你的XML吗?你能在
每个函数中设置警报($(this).text())
并验证“VII”是否收到警报吗?(或输出到控制台,或设置断点-或)$groupName应该在您的函数中是本地的-尝试“var$groupName”而不仅仅是“$groupName”nm,我是个白痴。。。我跳过了数组的最后一个元素。看起来不错
$xmlDoc.find('GroupName').each(function () {
$groupName = ($(this).text().length == 0) ? 'empty' : $(this).text();
if (groupnames.indexOf($groupName) < 0) {
groupnames.push($groupName);
}
});