freeCodeCamp-->js-->json嵌套数组

来源:互联网 发布:js中创建数组 编辑:程序博客网 时间:2024/05/12 21:12

https://www.freecodecamp.cn/challenges/accessing-nested-arrays-in-json

使用点操作符和中括号操作符来检索变量myPlants 的第二棵树。

代码如下:

// 初始化变量
var myPlants = [
  { 
    type: "flowers",
    list: [
      "rose",
      "tulip",
      "dandelion"
    ]
  },
  {
    type: "trees",
    list: [
      "fir",
      "pine",
      "birch"
    ]
  }  
];

var secondTree = myPlants[1].list[1]; 

运行结果 secondTree = pine