0

I do not want to display double the same product. If isbn repeats then do not display it double.

{
  "products": [{
    "ident": "001",
    "isbn": "2332",
    "discount": "10%",
    "bookstore": "library1"
  }, {
    "ident": "002",
    "isbn": "2332",
    "discount": "20%",
    "bookstore": "library2"
  }, {
    "ident": "003",
    "isbn": "3422",
    "discount": "30%",
    "bookstore": "library3"
  }, ]
}

function getData() {
  $.getJSON('data.json', function(data) { // get data from data.json
    var products = data.products;
    var tr = $("<tr>");
    var items = '';

    $.each(products, function(key, value) {
      items += "<tr position=" + value.bookstore + ">";
      items += "<td>" + value.ident + "</td>";
      items += "<td>" + value.isbn + "</td>";
      `enter code here`
      items += "<td>" + value.discount + "</td>";
      items += "<td>" + value.bookstore + "</td>";
      items += "</tr>";
    });

    $('#data').append(items); // show data in table
  });
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Piotr
  • 17
  • 1

3 Answers3

1

You could keep hold of the list of ISBN's in an array, and then check if the array contains an ISBN before doing something with it.

var bookList = [];

$.each(products, function(key, value) {

  // Pushes value.isbn to the bookList array
  bookList.push(value.isbn);

  // If the IBSN is not present in bookList then act as if it's a new entry instead of repeating it.
  if (!bookList.includes(value.isbn)) {
    // Do something with the data here.
  }
}
James Ives
  • 3,177
  • 3
  • 30
  • 63
  • 1
    Good approach. It is important to note that `Array.prototype.includes` is not currently supported in IE. If you choose to implement this approach, you will need to use the [polyfill](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes) as provided by _MDN_. – War10ck Jul 26 '17 at 14:15
  • Ah yes, good point! – James Ives Jul 26 '17 at 14:17
  • @JamesIves you may build the table on the fly and avoid to add rows containing duplicated values [fidde](https://jsfiddle.net/8kzmq02b/1/). What's your idea? – gaetanoM Jul 26 '17 at 14:26
0

You can accomplish this with a simple for loop. For the sake of this question, I hard-coded your JSON data. However, you can implement the ajax request with the $.getJSON() routine in it's place (as you did above) and the result will be the same:

var json = {
  "products": [{
    "ident": "001",
    "isbn": "2332",
    "discount": "10%",
    "bookstore": "library1"
  }, {
    "ident": "002",
    "isbn": "2332",
    "discount": "20%",
    "bookstore": "library2"
  }, {
    "ident": "003",
    "isbn": "3422",
    "discount": "30%",
    "bookstore": "library3"
  }, ]
};

var products = {};

for(var a = 0, len = json.products.length; a < len; a++) {
  if(products[json.products[a].isbn]) {
       products[json.products[a].isbn].bookstore = products[json.products[a].isbn].bookstore + "," + json.products[a].bookstore;
    } else {
      products[json.products[a].isbn] = json.products[a];
    }
}

$.each(products, function (key, value) {
 console.log(value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

This routine adds each product to a new products object. Each object key is the isbn number of the product. If the isbn already exists, the bookstores that the product is sold in is updated to include the previous value and the new value. If the isbn does not exist, the product is added to the products object as a new product.

War10ck
  • 12,387
  • 7
  • 41
  • 54
-1

Insert into an array the ISBN then check with indexOf if there exists in the array.

var arr = [

    {
        "ident":"001",
        "isbn":"2332",
        "discount":"10%",
        "bookstore": "library1"
    },

    {
        "ident":"002",
        "isbn":"2332",
        "discount":"20%",
        "bookstore": "library2"
    },

     {
        "ident":"003",
        "isbn":"3422",
        "discount":"30%",
        "bookstore": "library3"
    }
    ];

    var arrISBN = [];

    for(let index in arr) {

        if ( arrISBN.indexOf(arr[index].isbn) == -1  ) {
           arrISBN.push( arr[index].isbn);
            console.log("No duplicates");
        }


}