您好。让我解释一下我现在在做什么:
我正在从HTMLCollection(通过document.getElementsByClass((创建(创建数组
var arry = Array.from(zlec);
碰巧我数组中的每个项都是span,但它的innerHTML是一个指向如下文件的路径:
/PSO/Orders/2021/customer/ordernumber/grade/thickness/filenamewithextension
我想从每个项目中提取客户和订单号,所以我在数组中创建了数组:
for (j = 0; j < arry.length; j++) {
arry[j] = arry[j].innerHTML.split("/");
}
我可以使用这条干净的行(当然是循环(在一些调试div中显示它:
debug.innerHTML += "Customer: " + arry[j][4] + " - Order no.: " + arry[j][5] + "</br>";
这带来了美丽的结果:
Customer: AAA - Order no.: 0332
Customer: AAA - Order no.: 0332
Customer: AAA - Order no.: 0332
Customer: AAA - Order no.: 0377
Customer: AAA - Order no.: 0377
Customer: BBB - Order no.: 0350
问题是如何删除此结果中的重复项??
有可能使用这样的伪代码吗?如何在子数组中同时调用这两个项并与其他对进行比较?
For loop
{
div.innerHTML = arry[j][4]&[j][5]
if (arry[j+1][4]&[j+1][5] is equal to arry[j][4]&[j][5])
{
then do nothing and j++
}
else
{
div.innerHTML += arry[j+1][4]&[j+1][5] and j++
}
}
---------更新---------关于客户和数字的真实生活数据示例(暗示HTMLCollection(被审查
/PSO/Orders/2021/Customer W1/0324/1.4301/060/6_304_right.GEO
/PSO/Orders/2021/Customer E1/0318/1.4301/060/2405-K1-10.0.0.02.GEO
/PSO/Orders/2021/Customer E1/0318/1.4301/060/2405-K1-4.2.0.01.GEO
/PSO/Orders/2021/Customer E1/0318/1.4301/060/2405-K1-13.0.0.00_p3.GEO
/PSO/Orders/2021/Customer E1/0318/1.4301/060/2405-K1-1.0.0.01.GEO
/PSO/Orders/2021/Customer E1/0318/1.4301/060/2405-K1-1.6.0.03.GEO
/PSO/Orders/2021/Customer E1/0318/1.4301/060/2405-K1-1.7.0.02.GEO
/PSO/Orders/2021/Customer E1/0322/1.4301/060/2405-K2-5.0.0.02.GEO
/PSO/Orders/2021/Customer E1/0322/1.4301/060/2405-K2-6.0.0.01.GEO
/PSO/Orders/2021/Customer E1/0322/1.4301/060/2405-K2-5.0.0.03.GEO
/PSO/Orders/2021/Customer E1/0322/1.4301/060/2405-K2-7.0.0.01.GEO
/PSO/Orders/2021/Customer E1/0322/1.4301/060/2405-K2-1.3.0.00_p1.GEO
/PSO/Orders/2021/Customer O1/0394/1.4301/060/Curb_920.GEO
/PSO/Orders/2021/Customer E1/0374/1.4301/060/2400-K6-3.4.0.09.GEO
/PSO/Orders/2021/Customer P1/0384/1.4301/060/HKL-049_3.GEO
/PSO/Orders/2021/Customer P1/0384/1.4301/060/HKL-049_1.GEO
/PSO/Orders/2021/Customer P1/0384/1.4301/060/HKL-049_2.GEO
/PSO/Orders/2021/Customer P1/0384/1.4301/060/HKL-049_4.GEO
/PSO/Orders/2021/Customer E2/0382/1.4301/060/bracket_20.GEO
/PSO/Orders/2021/Customer E2/0382/1.4301/060/bracket_23.GEO
/PSO/Orders/2021/Customer E2/0382/1.4301/060/bracket_22.GEO
/PSO/Orders/2021/Customer E2/0382/1.4301/060/bracket_21.GEO
您可以使用临时Set
来获取唯一值。
我还将使用Array.from
回调参数来立即将HTML元素映射到它们的分割innerHTML
值,然后链接.map
来将这些子数组映射到最终显示字符串。
您可以通过简单的.join
:形成最终的HTML输出
var debug = document.getElementById("dbg");
var zlec = document.getElementsByClassName("path");
var arry = Array.from(zlec, elem =>
elem.innerHTML.split("/")
).map(([,,,,cust, ord]) =>
`Customer: ${cust} - Order no.: ${ord}`
);
debug.innerHTML = [...new Set(arry)].join("<br>");
<div class="path">/PSO/Orders/2021/Shell/12345678/grade/thickness/filenamewithextension</div>
<div class="path">/PSO/Orders/2021/NASA/829384751/grade/thickness/filenamewithextension</div>
<div class="path">/PSO/Orders/2021/Wallmart/927465822/grade/thickness/filenamewithextension</div>
<hr>
<div id="dbg"></div>
您可以在数组中使用reduce
方法返回一个新的唯一项数组
let result = arry.reduce((acc, i) => {
if(acc.find(a => a[4]===i[4] && a[5]===i[5]))
return acc;
acc.push(i);
return acc; },
[]);
console.log(result);