-1

I will be getting this JSON as response from server

var responsefromserver = 
    {
    "Restaurants": [
        {
            "RestrntArea": "Home"
        },
        {
            "RestrntArea": "Office"

        },
        {
            "RestrntArea": "Office"

        }
    ]
};

Could you please let me know , how to eliminate duplicates from the above while looping it in front end ??

This is my jsfiddle

http://jsfiddle.net/BreMW/720/

Anuj Garg
  • 584
  • 7
  • 22
Pawan
  • 31,545
  • 102
  • 256
  • 434
  • That's not JSON. If you're dealing with JavaScript source code, unless you're talking about something *in a string*, it's not JSON. – T.J. Crowder Jun 21 '15 at 17:46
  • Duplicate of http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array and so, so, so, so many others. Please search before posting. – T.J. Crowder Jun 21 '15 at 17:47
  • Hi @Preethi, what constitutes a duplicate? Duplicate key, duplicate value or both key and value being the same? – Greg B Jun 21 '15 at 17:47
  • Sorry for the confusion , duplicate key – Pawan Jun 21 '15 at 17:48

1 Answers1

2

Try this.

 var responsefromserver = {
        "Restaurants": [{
            "RestrntArea": "Home"
        }, {
            "RestrntArea": "Office"
        }, {
            "RestrntArea": "Office"
        }]
    };

    var html = [];
    for (var i = 0; i < responsefromserver.Restaurants.length; i++) {
        var name = responsefromserver.Restaurants[i].RestrntArea;
        if(html.indexOf(name)==-1)
              html.push(name);
    }

    alert(html.join(''));
Varun
  • 1,946
  • 2
  • 11
  • 17