javascript or jquery: Looping a multidimensional object

后端 未结 2 1762
野趣味
野趣味 2021-01-20 18:31

I just started playing around with JSON and I have created this example.

var shows = {

    \"ShowA\": 
                {   \"Date\"      : \"November 3-5, 2         


        
相关标签:
2条回答
  • 2021-01-20 18:42
    for(show in shows){
         console.log(shows[show]);
    }
    

    Fiddle: http://jsfiddle.net/maniator/Wp3N9/

    No extra libraries needed ^_^

    0 讨论(0)
  • 2021-01-20 18:57

    you can use a for ... in loop:

    for(var key in shows) {
        if (shows.hasOwnProperty(key)) {
            alert(shows[key].Date);
        }
    }
    

    It's important to note that an object has no sort order, but an array does. So if you wanted to sort by dates, you would need to use an array.

    Also it's good practice to use Object.hasOwnProperty

    0 讨论(0)
提交回复
热议问题