2

I am using JavaScript JSON object. I am not sure this is the right way to write JSON object :-

var address={
    details:{
        "martin":[{"full_name":"James Martin"},{"address":"Florida"},{"phone":"897657834"}],
        "luthar":[{"full_name":"Luther king"},{"address":"Boston"},{"phone":"9856568789"}],
        "jonson":[{"full_name":"Jonson vierra"},{"address":"New york"},{"phone":"98654567887"}]
    }
}

Now, I want to show every elements of each person in a table. I can do this manually by writing:

<table border=1>
<tr>
<td><script>document.write(address.details.martin[0].full_name)</script></td>
<td><script>document.write(address.details.martin[1].address)</script></td>
<td><script>document.write(address.details.martin[2].phone)</script></td>
</tr>
</table>

Like this for all 3 people, but I want to grab full details of every person by using a loop. How can I do this easily by using a loop?

7
  • You might want to think about using a binding library like knockout for something like this. Commented Apr 9, 2013 at 14:12
  • 1
    actually i don't wanna use any kinds of library right now. Cause i am newbie in javascirpt so i need to know from root. :) Commented Apr 9, 2013 at 14:14
  • Fair enough, but using libraries and learning JavaScript aren't mutually exclusive. Also don't get in the bad habit of inlining JavaScript like that. Finally, what you have isn't actually valid JSON since details isn't enclosed in quotes, but it's not clear why you want JSON rather than just an object literal anyway. Commented Apr 9, 2013 at 14:37
  • JSON is usually easy to use. And it can create array inside array inside array.. . So it's easy to decorate and call. Commented Apr 9, 2013 at 15:32
  • Not that easy apparently since you're JSON isn't valid, plus exactly the same is true of an object literal. It's actually pretty much exactly the same except you don't need to enclose the property names in quotes. Commented Apr 9, 2013 at 15:44

2 Answers 2

2

I would do something along these lines:

for (var i in address.details)
{
    document.write('<tr><td>' + address.details[i][0]['full_name'] + '</td><td>' + address.details[i][1]['address'] + '</td><td>' + address.details[i][2]['phone'] + '</td></tr>');
}

To follow up this code should output a table with the data you require:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>An XHTML 1.0 Strict standard template</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>

<body>
    <script>
    var address={
        details:{
        "martin":[{"full_name":"James Martin"},{"address":"Florida"},{"phone":"897657834"}],
        "luthar":[{"full_name":"Luther king"},{"address":"Boston"},{"phone":"9856568789"}],
        "jonson":[{"full_name":"Jonson vierra"},{"address":"New york"},{"phone":"98654567887"}]
        }
    }
    </script>
    <table border=1>
    <script>
        for (var i in address.details)
        {
            document.write('<tr><td>' + address.details[i][0]['full_name'] + '</td><td>' + address.details[i][1]['address'] + '</td><td>' + address.details[i][2]['phone'] + '</td></tr>');
        }
    </script>
    </table>
</body>

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

8 Comments

i believe address.details[i] is an array
sorry rushed code, always bad, I've edited it now. Should work.
I believe Rodrigo has a better answer for your situation though. Also I don't understand why you're putting script tags in the table cells, it's unecessary
@AlvinC Really a good and short answer. Nice ! but if i try to enter whole table. i mean including <table></table> tag. it is not writing. note: i wrote <table> tag outside of loop.
@slash-bang I've edited the answer to show you the exact script that you need. I would suggest changing the JSON if possible though.
|
1
for (var personName in address.details) {
  if (address.details.hasOwnProperty(personName)) {
    address.details[personName]
    //  [{"full_name":"James Martin"},{"address":"Florida"},{"phone":"897657834"}]
    // you could now loop through the personName's array
  }
}

Do you have control over the data structure at all? It seems a little convoluted?

1 Comment

actually i don't want full data in a person ( include ` bracket or "` . i just need such as martins full_name `James martin data in a table.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.