Traverse all the Nodes of a JSON Object Tree with JavaScript

前端 未结 16 1681
猫巷女王i
猫巷女王i 2020-11-22 06:26

I\'d like to traverse a JSON object tree, but cannot find any library for that. It doesn\'t seem difficult but it feels like reinventing the wheel.

In XML there are

16条回答
  •  爱一瞬间的悲伤
    2020-11-22 06:57

    function traverse(o) {
        for (var i in o) {
            if (!!o[i] && typeof(o[i])=="object") {
                console.log(i, o[i]);
                traverse(o[i]);
            } else {
                console.log(i, o[i]);
            }
        }
    }
    

提交回复
热议问题