从jquery中的xml数组中删除重复的项目

时间:2017-07-03 19:01:31

标签: javascript jquery ajax xml

我试图消除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");
    });
  });
});   

2 个答案:

答案 0 :(得分:1)

好吧,我试图改进你的代码并实现了所需的......

(只需阅读代码注释)

&#13;
&#13;
// Constants
var MANIFEST_XML_URL = 'https://cdn.shopify.com/s/files/1/0453/8489/t/26/assets/sample.xml';

// Event handlers
$(document).ready(function() {
  loadManifestXml();
});

/**
 * Used to load 'manifest' XML from server
 */
function loadManifestXml() {
  $.get(MANIFEST_XML_URL, onGetManifest);
}

/**
 * Used as a callback when loading 'manifest' XML from server
 *
 * @param {Object} response - The server response
 */
function onGetManifest(response) {
  // Parse and display data
  var data = getManifestData(response),
      html = getManifestHtml(data);

  if (html) {
     $('#content').html(html);
  }
}

/**
 * Used to marshal 'manifest' XML server response
 *
 * @param {Object} response - The server response
 * @returns {Object[]} data - An array of objects 
 */
function getManifestData(response) {
  var data;

  if (response) {
    data = $('manifest', response).map(function(i, manifest) {
      manifest = $(manifest);

      var cfg = {
          id: manifest.find('id').text(),
          po: manifest.find('po').text(),
          items: []
        },
        items = manifest.find('contents item');

      items.each(function(j, item) {
        item = $(item);

        cfg.items.push({
          sku: item.find('sku').text(),
          upc: item.find('upc').text()
        });
      });

      return cfg;
    });
  }

  return data || []; // Map can return 'null' so force it to be an 'Array'
}

/**
 * Used to build a HTML with 'manifest' data
 *
 * @param {Object[]} data - An array of objects
 * @returns {String} html
 */
function getManifestHtml(data) {
  var html = '';

  for (var i = 0; i < (data || []).length; ++i) {
    var obj = data[i];

    if (obj) {
      html = html + '<b>' + obj.id + ' ' + obj.po + '</b><br>';

      var items = obj.items;

      for (var j = 0; j < (items || []).length; ++j) {
        var item = items[j];

        if (item) {
            html = html + item.upc + ': ' + item.sku + '<br>';
        }
      }

      html = html + '<br>';
    }
  }

  return html;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content">
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

请考虑使用Object而不是Array:

&#13;
&#13;
jQuery(function($) {
  var customerInfo = {};

  $.get("https://cdn.shopify.com/s/files/1/0453/8489/t/26/assets/sample.xml", function(xml) {

    $('manifest', xml).each(function(n, m) {
      var id = $(m).find('id').text(),
        po = $(m).find('po').text();
      if (!customerInfo[id + ' ' + po]) customerInfo[id + ' ' + po] = [];

      $(m).find('item').each(function(n, i) {
        customerInfo[id + ' ' + po]
          .push({
            "UPC": $(i).find("upc").text(),
            "SKU": $(i).find("sku").text()
          })
      });
    });

    $("<div></div>").html(
      Object.keys(customerInfo).map(function(k) {
        return '<div class="title">' + k + '</div>' +
          customerInfo[k].map(function(i) {
            return '<span class="title">' + i.UPC + ": " + i.SKU + '</span><br>'
          }).join('')
      }).join('<br>')
    ).appendTo("#dvContent");
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="dvContent"></div>
&#13;
&#13;
&#13;

<div>追加到<p>也无效,所以我省略了它。