-1

I am a programing newbie and usually use Python but have found myself needing to use Javascript for this project (phonegap).

i retrieve the following query from a server

`[["CompanyName", "lat", "long", ID, "street", 6.8], ["CompanyName", "lat", "long", ID, "street", 23.7]]`

In Python i get would do the following to print CompanName for each entry.

for x in r.json():
        print x[0]

How would i achieve the same thing in Javascript?

when i retrieve the query if i try to loop through it, it iterates every character of the query as a string. so i try the following:

var result = xmlhttp.responseText;
result = eval("("+ result +")");

i loop through it with:

for (var i in result) 
                {
                    display +="<br/>" + result[i];}

How would i specify just the CompanyName like in the Python example?

`

Suman Bogati
  • 6,289
  • 1
  • 23
  • 34
4BY
  • 77
  • 12
  • 2
    Read [3. Use for-in correctly](http://stackoverflow.com/questions/9329446/for-each-in-an-array-how-to-do-that-in-javascript/9329476#9329476) – Grijesh Chauhan Feb 09 '14 at 18:39

1 Answers1

0

Use this result[i][0] since it's a nested array.

var result = [["CompanyName1", "lat", "long", "ID", "street", 6.8],["CompanyName2", "lat", "long", "ID", "street", 23.7]];

var div = document.getElementById('result');

for (var i = 0; i < result.length; i++) {
    div.innerHTML += ("<br/>" + result[i][0]);
}
<div id="result"></div>

Demo

theonlygusti
  • 11,032
  • 11
  • 64
  • 119
Jonathan
  • 8,771
  • 4
  • 41
  • 78