I'm looping through an json-like object and I'm trying to exclude a duplicate entry (the one with id:1) with no success. Here's my code:
var productData = '[{"id":1,"name":"Retro","price":10,"color":"blue"},{"id":2,"name":"Designer","price":12,"color":"red"},{"id":3,"name":"Classic","price":14,"color":"yellow"},{"id":1,"name":"Retro","price":10,"color":"blue"},{"id":4,"name":"Rimless","price":14,"color":"purple"},{"id":5,"name":"Glam & Sparkle","price":8,"color":"grey"}]',
data = $.parseJSON(productData),
table = $("#productTable"),
dataFiltered = [];
$.each(data, function(key, value) {
if ($.inArray(value.id, dataFiltered) === -1) {
dataFiltered.push({"id": value.id, "price": value.price, "name": value.name,"color": value.color});
}
});
console.log(dataFiltered)
$.each(dataFiltered, function(key, value) {
table.append("<div class='row'><span>£"+value.price+"</span>"+
"<span>"+value.name+"</span>"+"<span>"+value.color+
"</span></div>");
});
What am I doing wrong? It returns the same object with the duplicate entry in it. here's the fiddle demo too.
Thanks in advance.