打字稿:有没有办法获得具有嵌套属性的Javascript对象中的所有键?

时间:2017-01-30 03:25:19

标签: javascript recursion javascript-objects

例如,如果我有

{
   "key1":{
      "key2":[
         {
            "key3":[
               {
                  "key4":{
                     "key5":0
                  }
               },
               {
                  "key6":{
                     "key7":""
                  }
               }
            ]
         },
         {
            "key8":{
               "key9":true
            }
         }
      ]
   }
}

有没有办法获得这样的所有键?

["key1", "key2", "key3", "key4", "key5", "key6", "key7", "key8", "key9"]

编辑:我在这里尝试了suggestion,但它没有成功Typescript: what could be causing this error? "Element implicitly has an 'any' type because type 'Object' has no index signature"

1 个答案:

答案 0 :(得分:1)

你需要一个递归函数来迭代。

试试这个

{{1}}