Traverse all nodes in the JSON JavaScript object Tree

Source: Internet
Author: User

I want to traverse the JSON object tree, but why can't I find any library. It seems to be not difficult, but it feels like reinventing the wheel.

There are many tutorials in XML that show how to traverse the XML DOM Tree: (


Workaround 1:

If you think what JQuery is,
Overkill
With this primitive task, you can do this:


Your objectvar o = { foo: "Bar", arr:[1,2,3], SuBo: { foo2: "Bar2" }};//called with every property And it ' s valuefunction process (key,value) { log (key + ":" +value);} function Traverse (o,func) {for (i in O) { func.apply (This,[i,o[i]]); if (typeof (O[i]) = = "Object") {//going on step ' down ' object tree!! Traverse (O[i],func); }} That's all ... no magic, no bloated frameworktraverse (o,process);

Workaround 2:

A JSON object is a simple Javascript object. This is actually the idea of JSON: JavaScript object notation. So you will traverse the JSON object, but you will choose to "traverse" the Javascript object generally.

Jquery I would like to do


$.each (Myjsonobj, function (key,val) { //do something with key and Val});

You can always write a function to descend to an object recursively:


function Traverse (jsonobj) { if (typeof jsonobj = = "Object") { $.each (jsonobj, function (k,v) { //k is either An array index or object key traverse (v); } } else { //Jsonob is a number or string }}

This should be a good starting point. I strongly recommend using JQuery for things like this, because their utilities, such as in each loop, make it much easier to write such code.


Workaround 3:

There is a new library of JavaScript that traverses JSON data to support many different usage scenarios.

Http://github.com/substack/js-traverse

It applies to all kinds of JavaScript objects. It can even detect cycles.

It also provides the path for each node.


Workaround 4:

Depends on what you want to do. The following is an example of traversing the JavaScript object tree, printing keys, and values:


function Js_traverse (o) { var type = typeof o if (type = = "Object") { for (var key in O) { print ("Key:", Key) Js_traverse (O[key]) } } else { print (o) }}js> foobar = {foo: "Bar", Baz: "Quux", Zot: [1, 2, 3 , {some: "hash"}]}[object object]js> js_traverse (foobar) key: Foobarkey: Bazquuxkey: Zotkey: 01key: 12key: 23key: 3key: somehash

Workaround 5:

There is a missing} in the above wire function. Corrected version:

function Traverse (O,func) {for (i in O) {func.apply (this,[i,o[i]]);
if (typeof (O[i]) = = "Object") {//going in the object tree to the next!! Traverse (O[i],func);}}}

Traverse all nodes in the JSON JavaScript object Tree

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.