0
我是JS和JSON的新手。我有這樣基於值的權重從JSON對象中刪除重複值
{"q1":[{"name":"p1","weight":64},{"name":"p3","weight":56}],"q2":[{"name":"p1","weight":56},{"name":"p2","weight":56},{"name":"p4","weight":56},{"name":"p5","weight":56},{"name":"p6","weight":64}],"q3":[{"name":"p1","weight":48},{"name":"p2","weight":64},{"name":"p3","weight":64}],"q4":[{"name":"p1","weight":64},{"name":"p1","weight":106},{"name":"p2","weight":56},{"name":"p3","weight":56},{"name":"p3","weight":112}],"q5":[{"name":"p1","weight":64},{"name":"p1","weight":113},{"name":"p2","weight":49},{"name":"p3","weight":56},{"name":"p3","weight":56},{"name":"p4","weight":49},{"name":"p5","weight":49},{"name":"p6","weight":56}],"q6":[]}
我想刪除重複的值,只保留單值與像第四季度最大的權重JSON對象我有兩個P1值。但我只需要1這裏是106
我想這樣
{"q1":[{"name":"p1","weight":64},{"name":"p3","weight":56}],"q2":[{"name":"p1","weight":56},{"name":"p2","weight":56},{"name":"p4","weight":56},{"name":"p5","weight":56},{"name":"p6","weight":64}],"q3":[{"name":"p1","weight":48},{"name":"p2","weight":64},{"name":"p3","weight":64}],"q4":[{"name":"p1","weight":106},{"name":"p2","weight":56},{"name":"p3","weight":112}],"q5":[{"name":"p1","weight":113},{"name":"p2","weight":49},{"name":"p3","weight":56},{"name":"p4","weight":49},{"name":"p5","weight":49},{"name":"p6","weight":56}],"q6":[]}
你嘗試到對象的最大重量[搜索谷歌(HTTPS:/ /www.google.com/search?q=Remove+duplicate+value+from+JSON+object)與您的標題? – mplungjan
創建一個使用該名稱作爲關鍵字的對象,該值爲重量。循環訪問原始數組,測試當前元素的權重是否大於對象元素的權重。 – Barmar