如何在JQuery中从JSON中删除重复的节点 [英] How to remove duplicate nodes from JSON in JQuery

查看:74
本文介绍了如何在JQuery中从JSON中删除重复的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字符串



I have following string

[{"text":"menu1","parent":"#","id":"128102"},{"text":"menu1.1","parent":"128102","id":"128103"},{"text":"menu1.1","parent":"128102","id":"128103"}]



我希望它为




and i want it as

[{"text":"menu1","parent":"#","id":"128102"},{"text":"menu1.1","parent":"128102","id":"128103"}]


Jquery中的


in Jquery

推荐答案

什么都没有内置的...你必须运行一个循环检查重复...

这里有一个样本供你开始...

There is nothing built in for that...You must run a loop to check duplicates...
Here a sample for you to start with...
var json = [
    {"text":"menu1","parent":"#","id":"128102"},
    {"text":"menu1.1","parent":"128102","id":"128103"},
    {"text":"menu1.1","parent":"128102","id":"128103"}
];

var ids = [];
var clean = [];


.each(json,函数(索引,值){
if
.each(json, function(index, value) { if(


.inArray(value.id,ids) )== -1)
{
ids.push(value.id);
clean.push(value);
}
});
.inArray(value.id, ids) == -1) { ids.push(value.id); clean.push(value); } });



ids数组是检查重复项的辅助工具,只包含原始数组中的唯一ID,其中clean将保存实际数组而不重复...


The ids array is a helper to check for duplicates and contains only the unique ids from the original array, where clean will hold the actual array with no duplicates...


这篇关于如何在JQuery中从JSON中删除重复的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆