2017-07-26 54 views
0

我不想顯示同一產品的兩倍。如果isbn重複,則不要將其顯示爲雙倍。從json jquery中刪除位置

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

爲什麼你的問題描述有太多重複? – GibboK

+2

不錯的口頭禪!知道哪個被保存或丟棄的規則是什麼?第一個? – Kaddath

+0

我多次重複添加錯誤。對不起 – Piotr

回答

-1

插入到數組中,然後在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"); 
     } 


} 
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

好方法。需要注意的是,IE中目前不支持'Array.prototype.includes'。如果您選擇實施此方法,您將需要使用由_MDN_提供的[polyfill](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes) 。 – War10ck

+0

啊,是的,好點! –

+0

@JamesIves你可以隨時建立表格,並避免添加包含重複值的行[fidde](https://jsfiddle.net/8kzmq02b/1/)。你的想法是什麼? – gaetanoM

0

您可以通過簡單的for循環完成此操作。爲了這個問題,我對你的JSON數據進行了硬編碼。然而,可以實現在它的地方$.getJSON()常規Ajax請求(如前所示),其結果將是相同的:

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++) { 
 
\t \t if(products[json.products[a].isbn]) { 
 
    \t products[json.products[a].isbn].bookstore = products[json.products[a].isbn].bookstore + "," + json.products[a].bookstore; 
 
    } else { 
 
    \t \t products[json.products[a].isbn] = json.products[a]; 
 
    } 
 
} 
 

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

這個程序把每個產品一個新的products對象。每個對象關鍵字都是該產品的編號isbn。如果isbn已經存在,則產品銷售的書店會更新爲包含以前的值和新的值。如果isbn不存在,則作爲新產品將該產品添加到products對象中。

+0

非常感謝。太好了 – Piotr