直接上代码:
function jsonquchong() {
var $a ={"code": 0,
"data": {"record": [
{"name": "数据1", "class": 2, "create_time": "2019-08-21 10:39:36"},
{"name": "数据1", "class": 3, "create_time": "2019-08-21 10:39:37"}
]}}
$b = {"rows":[]};
var arr = [];
for(var i=0; i<$a.data.record.length; i++){
if(arr.indexOf($a.data.record[i].name) ==-1){
arr.push($a.data.record[i].name);
$b.rows.push($a.data.record[i]);
}
}
var newarr = $b;
$("#span").html(JSON.stringify(newarr))
}
<button onclick="jsonquchong()">json去重</button>
<span id="span"></span>
效果如下:
- 未点击时:
- 点击json去重后: