-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?

Prashanth Benny
  • 1,523
  • 21
  • 33
Fullstack
  • 135
  • 1
  • 1
  • 11
  • "make display them" ... ? use `console.log`? – evolutionxbox Sep 26 '16 at 13:56
  • JSON.stringify(data) – ADyson Sep 26 '16 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? – Carl Sep 26 '16 at 13:57
  • 4
    _“I have this array”_ – that is not an array, it’s an object. – CBroe Sep 26 '16 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. – JJJ Sep 26 '16 at 13:58
  • @CBroe This guy is likely more familiar with PHP, where arrays have key value pairs, and are converted to objects in JSON. – Howzieky Sep 26 '16 at 13:59
  • @Howzieky what's the relevance to this issue? php arrays don't use `length` and op is clearly not looking for the outer array if that was the case anyway – charlietfl Sep 26 '16 at 14:14
  • I suppose it's not relevant to the issue (though it may help OP with his google searches in the future). I was just explaining why OP might have called it an array instead of an object, and where the confusion would come from – Howzieky Sep 26 '16 at 17:52
  • This is pretty close: https://stackoverflow.com/a/44249809/2943403 – mickmackusa Jan 19 '19 at 00:46

1 Answers1

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;
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Fullstack
  • 135
  • 1
  • 1
  • 11
  • 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. – mickmackusa Jan 19 '19 at 00:23