有关获取密钥JSON多类的问题

时间:2019-01-23 03:35:41

标签: javascript json

我对JSON和JavaScript有一些问题,因为我只是一个初学者。 现在,我尝试foreach这个json的全部keys["name"]

var l = [{
 "pages": [
  {
   "name": "Scan",
   "elements": [
    {
     "type": "radiogroup",
     "name": "Gender",
     "title": "Gender",
     "choices": [
      "Male",
      "Female"
     ]
    },
    {
     "type": "text",
     "name": "Name",
     "title": "Name"
    },
    {
     "type": "text",
     "name": "Age",
     "inputType": "number"
    },
    {
     "type": "text",
     "name": "E-mail",
     "title": "E-mail",
     "validators": [
      {
       "type": "email"
      }
     ],
     "inputType": "email"
    }
   ]
  },
  {
   "name": "Q",
   "elements": [
    {
     "type": "radiogroup",
     "name": "What Country Food you like?",
     "choices": [
      "Thailand",
      "Japan",
      "Italy"
     ],
     "colCount": 3
    },
    {
     "type": "text",
     "name": "What is your favorite food",
     "visibleIf": "{What Country Food you like?} notempty"
    }
   ]
  }
 ]
}];

我尝试使用

    l.forEach(function(element) {
         console.log(element);       
});

但是我在console.log中只得到2个数组

0:Array[name:"Scan", element:Array(4)}
1:Array[name:"Q", element:Array(2)}

我只需要keys["name"]像这样

(Scan, Name, Age, Gender, E-mail, "What Country Food you like?", "What is your favorite food")

1 个答案:

答案 0 :(得分:0)

现在我有了自己的答案 谢谢大家

l[0].pages[0].elements[0];