我正在使用JSON数组,我正在尝试删除在运行循环时在HTML页面上发布的重复项。目前我在页面上看到两个图像都是相同类别的披萨。我该如何制作它只显示一个带图像的披萨类别?我想隐瞒其他人。 希望有人能告诉我我需要做什么。以下是我的代码:
TransferManager

(function() {
'use strict';
var url = 'path to json';
$.getJSON(url, function(json) {
var data = (json);
var images = '';
var uniqueNames = [];
//retrieve values from json file
$.each(data, function(i, item) {
if($.inArray(item, uniqueNames) === -1) uniqueNames.push(item)
images += '<div class="col-md-6 col-sm-6">' + '<img class="img img-responsive img-hover imgCategory" src="' + (item[0].imageURL) + '">' + '<h1>' + (item[0].category) + '</h1>' + '</div>';
});
//append results to div
$('#images').html(images);
});
})();
答案 0 :(得分:0)
尝试这种方法:
var arr = [], //collect id values
collection = []; //collect unique object
$.each(json_all, function (index, value) {
if ($.inArray(value.id, arr) == -1) { //check if id value not exits
arr.push(value.id);//push id value in arr
collection.push(value); //put object in collection to access later
}
});