从json jquery中删除位置

时间:2017-07-26 13:49:29

标签: javascript jquery

我不想显示双倍相同的产品。如果isbn重复,那么不要显示它加倍。

{
  "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
  });

3 个答案:

答案 0 :(得分:1)

您可以保留数组中ISBN的列表,然后在使用它之前检查数组是否包含ISBN。

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.
  }
}

答案 1 :(得分:0)

您可以使用简单的for循环来完成此操作。为了这个问题,我对您的JSON数据进行了硬编码。但是,您可以在其中使用$.getJSON()例程来实现ajax请求(如上所述),结果将是相同的:

&#13;
&#13;
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);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

此例程将每个产品添加到新的products对象。每个对象键都是产品的isbn个数字。如果isbn已存在,则销售产品的书店将更新为包含先前值和新值。如果isbn不存在,则会将产品作为新产品添加到products对象中。

答案 2 :(得分:-1)

将ISBN插入数组,然后使用indexOf检查数组中是否存在。

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");
        }


}