0

Demo and complete code is like this : https://jsfiddle.net/oscar11/44L4z5e0/

My Javascript code is like this :

var json = {
    "SearchAvailResponse": {
        "Hotel": [{
            "HCode": "IDJKT_00393",
            "Name": "All Seasons Jakarta Gajah Mada (Opening July 2013)",
        }, {
            "HCode": "IDJKT_00448",
            "Name": "Holiday Inn Express Jakarta Pluit"
        }, {
            "HCode": "IDJKT_00448",
            "Name": "Holiday Inn Express Jakarta Pluit"
        }, {
            "HCode": "IDJKT_00448",
            "Name": "Holiday Inn Express Jakarta Pluit"
        }, {
            "HCode": "IDJKT_00448",
            "Name": "Holiday Inn Express Jakarta Pluit"
        }, {
            "HCode": "IDJKT_00337",
            "Name": "Novotel Gajah Mada"
        }, {
            "HCode": "IDJKT_00337",
            "Name": "Novotel Gajah Mada"
        }, {
            "HCode": "IDJKT_00344",
            "Name": "Rota International"
        }]
    }
};


var totalHotel = json.SearchAvailResponse.Hotel.length;
// console.log(totalHotel);
var hotel = '';
for(var i=0;i<totalHotel;i++){
    console.log(json.SearchAvailResponse.Hotel[i].Name);
    hotel += json.SearchAvailResponse.Hotel[i].Name;
    hotel += '<br/>';
}

$('#hotel').html(hotel);

The result of value that is displayed is double data

I want the result like this :

All Seasons Jakarta Gajah Mada (Opening July 2013)

Holiday Inn Express Jakarta Pluit

Novotel Gajah Mada

Rota International

Any solution to solve my problem?

Thank you

1
  • if this is from database i would suggest to fix the query and not the json..if not from database please disregard this comment Commented Mar 2, 2016 at 7:55

7 Answers 7

5

You can use this. Use an array to check if the value has already read.

Use hotelArray.indexOf(val) == -1 to check if value exist in array.

var totalHotel = json.SearchAvailResponse.Hotel.length;
// console.log(totalHotel);
var hotel = '';
var hotelArray = [];
for (var i = 0; i < totalHotel; i++) {
  val = json.SearchAvailResponse.Hotel[i].Name;
  console.log(json.SearchAvailResponse.Hotel[i].Name);
  if (hotelArray.indexOf(val) == -1) {
    hotelArray.push(json.SearchAvailResponse.Hotel[i].Name);
  }
}

$('#hotel').html(hotelArray.join('<br/>'));

DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

I need you help. look here : stackoverflow.com/questions/37209847/…
1
var totalHotel = json.SearchAvailResponse.Hotel.length;
// console.log(totalHotel);
var hotel = '';
var hotels = [];
for(var i=0;i<totalHotel;i++){
        if(jQuery.inArray(json.SearchAvailResponse.Hotel[i].Name, hotels) == -1)
    {
      hotels.push(json.SearchAvailResponse.Hotel[i].Name);
      hotel += json.SearchAvailResponse.Hotel[i].Name;
      hotel += '<br/>';    
    }
}

Demo -> https://jsfiddle.net/83dbha0h/

Comments

1

You can use jQuery's each() to simplify the looping, and you can use object properties as a quick way to register what hotels have already been seen.

Here's the code to make it work:

var allHotels = json.SearchAvailResponse.Hotel;
var seenHotels = {};
$(allHotels).each(function(i, hotel) {
    if (!seenHotels[hotel.Name]) {
        $("#hotel").append("<p>" + hotel.Name + "</p>");
    }
    seenHotels[hotel.Name] = true;
});

You can see that in action in this Fiddle: https://jsfiddle.net/fzaey8jw/

Comments

1

first create an object (means a hashmap)

var hotels = json.SearchAvailResponse.Hotel.reduce(function(map, obj) {
    map[obj.Name] = obj.HCode;
    return map;
}, {});

after that you can do this:

var hotel = Object.keys(hotels).sort().join('<br/>');

Comments

1

You can do like this

var json = {
    "SearchAvailResponse": {
        "Hotel": [{
            "HCode": "IDJKT_00393",
            "Name": "All Seasons Jakarta Gajah Mada (Opening July 2013)",
        }, {
            "HCode": "IDJKT_00448",
            "Name": "Holiday Inn Express Jakarta Pluit"
        }, {
            "HCode": "IDJKT_00448",
            "Name": "Holiday Inn Express Jakarta Pluit"
        }, {
            "HCode": "IDJKT_00448",
            "Name": "Holiday Inn Express Jakarta Pluit"
        }, {
            "HCode": "IDJKT_00448",
            "Name": "Holiday Inn Express Jakarta Pluit"
        }, {
            "HCode": "IDJKT_00337",
            "Name": "Novotel Gajah Mada"
        }, {
            "HCode": "IDJKT_00337",
            "Name": "Novotel Gajah Mada"
        }, {
            "HCode": "IDJKT_00344",
            "Name": "Rota International"
        }]
    }
};
k={};
var totalHotel = json.SearchAvailResponse.Hotel.length;
for(i=0;i<totalHotel;i++){
  k[json.SearchAvailResponse.Hotel[i].Name]=0;
}
var hotel=Object.keys(k).join("<br/>");
$('#hotel').html(hotel);

Here I am making a object again but key of object will be name of Hotels since key can'not be duplicate in object so its taking only once next i am joining those key with and getting result. Hope it will Help

Comments

0

Here you go just need to check whether the data is repeated before appending it to hotel array: here is a demo https://jsfiddle.net/44L4z5e0/2/

just add

var uniqueNames = [];
    for(i = 0; i<totalHotel; i++){    
        if(uniqueNames.indexOf(json.SearchAvailResponse.Hotel[i].Name) === -1){
            uniqueNames.push(json.SearchAvailResponse.Hotel[i].Name);        
        }        
    }

jQuery

var json = {

        "SearchAvailResponse": {
            "Hotel": [{
                "HCode": "IDJKT_00393",
                "Name": "All Seasons Jakarta Gajah Mada (Opening July 2013)",
            }, {
                "HCode": "IDJKT_00448",
                "Name": "Holiday Inn Express Jakarta Pluit"
            }, {
                "HCode": "IDJKT_00448",
                "Name": "Holiday Inn Express Jakarta Pluit"
            }, {
                "HCode": "IDJKT_00448",
                "Name": "Holiday Inn Express Jakarta Pluit"
            }, {
                "HCode": "IDJKT_00448",
                "Name": "Holiday Inn Express Jakarta Pluit"
            }, {
                "HCode": "IDJKT_00337",
                "Name": "Novotel Gajah Mada"
            }, {
                "HCode": "IDJKT_00337",
                "Name": "Novotel Gajah Mada"
            }, {
                "HCode": "IDJKT_00344",
                "Name": "Rota International"
            }]
        }
    };


    var totalHotel = json.SearchAvailResponse.Hotel.length;
    // console.log(totalHotel);
    var hotel = '';

        var uniqueNames = [];
for(i = 0; i<totalHotel; i++){    
    if(uniqueNames.indexOf(json.SearchAvailResponse.Hotel[i].Name) === -1){
        uniqueNames.push(json.SearchAvailResponse.Hotel[i].Name);        
    }        
}

for(i = 0; i< uniqueNames.length; i++){    
    console.log(uniqueNames[i]);      
    hotel+=uniqueNames[i]+"<br>";
}


    $('#hotel').html(hotel);

Comments

0

Have you tried by simply check if the name is already in your generated html?

var totalHotel = json.SearchAvailResponse.Hotel.length;
    // console.log(totalHotel);
    var hotel = '';
    for(var i=0;i<totalHotel;i++){
        console.log(json.SearchAvailResponse.Hotel[i].Name);
        var thisHotel = json.SearchAvailResponse.Hotel[i].Name;
        if(hotel.indexOf(thisHotel) === -1){
           hotel += thisHotel;
           hotel += '<br/>';
        }
    }

    $('#hotel').html(hotel);

this is working, in your jsfiddle the return is:

All Seasons Jakarta Gajah Mada (Opening July 2013)
Holiday Inn Express Jakarta Pluit
Novotel Gajah Mada
Rota International

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.