Click here to Skip to main content
15,921,295 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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"}]

in Jquery
Posted
Updated 5-Jun-20 2:45am
v2

function removeDuplicates(json_all) {
var arr = [],
collection = [];

$.each(json_all, function (index, value) {
if ($.inArray(value.id, arr) == -1) {
arr.push(value.id);
collection.push(value);
}
});
return collection;
}
 
Share this answer
 
Comments
CHill60 5-Jun-20 8:48am    
No different to Solution 1 posted 5 years ago. Copying other members work and posting it as your own can lead to your account being closed. I suggest you delete this post.
There is nothing built in for that...You must run a loop to check duplicates...
Here a sample for you to start with...
JavaScript
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, function(index, value) {
    if($.inArray(value.id, ids) == -1)
    {
        ids.push(value.id);
        clean.push(value);
    }
});

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...
 
Share this answer
 
v2
Comments
Devraj Kapdi 10-Mar-15 7:38am    
Thank u Peter. i tried before with the same but it wont helped me . you solved my issue. thank u.
Kornfeld Eliyahu Peter 10-Mar-15 7:41am    
You're welcome...
Hafiz Muhammad Farooq 1-Jan-18 3:28am    
Peter, A perfect solution to remove duplicates. Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900