最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript Multi-level array of JSON objects - how to access key-value pair in second level or higher - Stack Overflow

programmeradmin0浏览0评论

Consider the following array of JSON objects:

myList = [
    {title:"Parent1",
        children:[{childname:"Child11"},
                     {childname:"Child12"}],
        cars:[{carname:"Car11"},
              {carname:"Car12"}]
    },
    {title:"Parent2",
        children:[{childname:"Child21"},
                     {childname:"Child22"}],
        cars:[{carname:"Car21"},
              {carname:"Car22"}]
    }
];

How does one access the "Child21" in javascript? The following options didn't work:

var myString = myList[1].children[0].childname; //Does not work
var myString = myList[1]["children"][0].childname; //Does not work

Consider the following array of JSON objects:

myList = [
    {title:"Parent1",
        children:[{childname:"Child11"},
                     {childname:"Child12"}],
        cars:[{carname:"Car11"},
              {carname:"Car12"}]
    },
    {title:"Parent2",
        children:[{childname:"Child21"},
                     {childname:"Child22"}],
        cars:[{carname:"Car21"},
              {carname:"Car22"}]
    }
];

How does one access the "Child21" in javascript? The following options didn't work:

var myString = myList[1].children[0].childname; //Does not work
var myString = myList[1]["children"][0].childname; //Does not work
Share Improve this question edited Oct 30, 2009 at 14:26 user181548 asked Oct 30, 2009 at 14:24 PS2009PS2009 511 gold badge1 silver badge2 bronze badges 5
  • 2 UGH*** Code formatting please! – Zoidberg Commented Oct 30, 2009 at 14:26
  • 3 Your first example myList[1].children[0].childname does work. – Crescent Fresh Commented Oct 30, 2009 at 14:26
  • You mean carname... right crecentfresh... Put that in an answer and I will vote for it – Zoidberg Commented Oct 30, 2009 at 14:27
  • @Zoidberg: not sure, question is looking for "Child21" I think. – Crescent Fresh Commented Oct 30, 2009 at 14:32
  • Your first option is correct; if it's not working on your set-up, the problem is not with your code (as written). "Children" is a bad choice, probably, for its's ubiquity of use; are you using any libraries that are interfering due to naming conventions? – Jonline Commented May 14, 2014 at 22:33
Add a comment  | 

4 Answers 4

Reset to default 9

This worked OK for me:

myList[1].children[0].childname

This is also OK:

myList[1]["children"][0].childname;

In full,

<html>
<body>
<script>
var myList = [
    {title:"Parent1",
        children:[{childname:"Child11"},
                     {childname:"Child12"}],
        cars:[{carname:"Car11"},
              {carname:"Car12"}]
    },
    {title:"Parent2",
        children:[{childname:"Child21"},
                     {childname:"Child22"}],
        cars:[{carname:"Car21"},
              {carname:"Car22"}]
    }
];
alert (myList[1].children[0].childname);
</script>
</body>
</html>

var myString = myList[1].children[0].childname;

In FireFox's Firebug works

This does work...

alert(myList[1].children[0].childname);

Your first option...

var myString = myList[1].children[0].childname;

should work just fine.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论