-4

I have this array:

{"Los Angeles, CA":["East Los Angeles","Florence","Florence-Firestone","Los Feliz","West Los Angeles"]}

But my code prints only "Los Angeles, CA", without child array strings...

function search4Location(query = true) {
    $.ajax({
        url: '/work/ajax/regions.php' + (query ? '?q=' + $("#searchLocation").val() : ''),
        dataType: 'json',
        success: function(data) {
            var datalen = data.length;
            $("#region").html('');
            if (query == true) {
                for (var i = 0; i < datalen; i++) {alert(123);
                    $("#region").append('<option>' + data[i] + '</option>');
                    var datalen2 = data[i].length;
                    for (var ii = 0; ii < datalen2; ii++) {
                        $("#region").append('<option>—— ' + data[i][ii] + '</option>');
                    }
                }
            } else {
                for (var i = 0; i < datalen; i++) {
                    $("#region").append('<option>' + data[i] + '</option>');
                }
            }
        } 
    });

    return false;
}

How to display them?

9
  • "make display them" ... ? use console.log? Commented Sep 26, 2016 at 13:56
  • JSON.stringify(data) Commented Sep 26, 2016 at 13:57
  • Is the query var meant to relate to anything? I don't see it being set. You probably want to check the return is an array, the iterate it if so, if not print it? Commented Sep 26, 2016 at 13:57
  • 4
    “I have this array” – that is not an array, it’s an object. Commented Sep 26, 2016 at 13:57
  • 1
    Given that data, the code shouldn't do anything because it's an object which doesn't have the length property. Commented Sep 26, 2016 at 13:58

1 Answer 1

0

I found the way!

function search4Location(query = true) {
    $.ajax({
        url: '/work/ajax/regions.php' + (query ? '?q=' + $("#searchLocation").val() : ''),
        dataType: 'json',
        success: function(data) {
            $("#region").html('');
            if (query == true) {
                for (var make in data) {
                    $("#region").append('<option>' + make + '</option>');
                    for (var i = 0; i < data[make].length; i++) {
                        $("#region").append('<option>— ' + data[make][i] + '</option>');
                    }
                }
            } else {
                var datalen = data.length;
                for (var i = 0; i < datalen; i++) {
                    $("#region").append('<option>' + data[i] + '</option>');
                }
            }
        } 
    });

    return false;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Please include an explanation with every answer that you post. Stack Exchange sites are more than just collections of snippets. The goal is to start with correct solutions then educate researchers so that they can apply the gained knowledge to more than just the narrow scope of the question.

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.