JQuery and JSON: getting an element by name

I have the following JSON:

var json = {"system": {"World": {"actions": {"Hello": {"src": "hello world / hello world.js", "command": "helloWorld"}}}}}

I have the following javascript:

var x = "system";
// get the contents of the system by doing something like json.getElementByName (x)

How to get system contents using json and x in jQuery?

+4
source share
3 answers

Just use:

 var x = "system"; json[x]; 

This is a key/value search system and does not need a function call to use it.

+8
source

Well, as far as I know, jQuery does not move such objects - just the DOM. You can write a little function to do this:

 function findSomething(object, name) { if (name in object) return object[name]; for (key in object) { if ((typeof (object[key])) == 'object') { var t = findSomething(object[key], name); if (t) return t; } } return null; } 

It should be obvious that I did not use this feature through a complex QA process.

+4
source

Try using JSON Path , this is similar to XPath expression.

+1
source

Source: https://habr.com/ru/post/1300924/


All Articles