【发布时间】:2017-07-03 19:01:30
【问题描述】:
我正在尝试消除 xml 输出中的重复元素。循环有效,但我需要跳过除第一个循环之外的所有循环中重复的 ID 和 PO 值。
现在,输出如下所示:
2 123456
1234567890:AA100
2 123456
0987654321:BB100
3 123456
11223345:CC00
3 123456
554433221:DD100
我试图让它看起来像:
2 123456
1234567890:AA100
0987654321:BB100
3 123456
11223345:CC00
554433221:DD100
这是我的代码 - 您可以在此处查看示例: https://codepen.io/BIGREDBOOTS/pen/VWQNBw
jQuery(function ($) {
$("#dvContent").append("<p class='mainblock'></p>");
var customerInfo = [];
$.get("https://cdn.shopify.com/s/files/1/0453/8489/t/26/assets/sample.xml", function(xml) {
$('manifest', xml).each(function() {
var id = $(this).find('id').text();
var po = $(this).find('po').text();
var loc = {
"UPC": $(this).find("upc").first().text(),
"Contents": {
"Item": $.map($(this).find("item").toArray(), function(manifest) {
return {
"ID": id,
"PO": po,
"UPC": $(manifest).find("upc").text(),
"SKU": $(manifest).find("sku").text()
};
})
}
};
customerInfo.push(loc);
$("<div></div>").html($.map($(loc.Contents.Item).toArray(), function(manifest) {
return '<div class="title">' + manifest.ID + ' ' + manifest.PO + '</div>' +
'<span class="title">' + manifest.UPC + ": " + manifest.SKU + '</span> <br/>' ;
}).join("")).append("<br>").appendTo("#dvContent p");
});
});
});
【问题讨论】:
标签: javascript jquery ajax xml