/    Sign up×
Community /Pin to ProfileBookmark

Travers in Javascript

Hi, I am starting with Javascript and I want to know how I can do dynamically travers of json in javascript? Anybody know? For example I have this json format:var x = {
“condition”: “AND”,
“rules”: [{
“id”: “price”,
“field”: “price”,
“type”: “double”,
“input”: “number”,
“operator”: “less”,
“value”: 10.25
},
{
“condition”: “OR”,
“rules”: [{
“id”: “category”,
“field”: “category”,
“type”: “integer”,
“input”: “select”,
“operator”: “equal”,
“value”: 2
},
{
“id”: “category”,
“field”: “category”,
“type”: “integer”,
“input”: “select”,
“operator”: “equal”,
“value”: 1
},
{
“id”: “price”,
“field”: “price”,
“type”: “double”,
“input”: “number”,
“operator”: “equal”,
“value”: 10000
}
]
}
],

“valid”: true
}

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumSep 03.2020 — You can use this code:
<script>
var x = {
"condition": "AND",
"rules": [{
"id": "price",
"field": "price",
"type": "double",
"input": "number",
"operator": "less",
"value": 10.25
},
{
"condition": "OR",
"rules": [{
"id": "category",
"field": "category",
"type": "integer",
"input": "select",
"operator": "equal",
"value": 2
},
{
"id": "category",
"field": "category",
"type": "integer",
"input": "select",
"operator": "equal",
"value": 1
},
{
"id": "price",
"field": "price",
"type": "double",
"input": "number",
"operator": "equal",
"value": 10000
}
]
}],
"valid": true
}
function traversIt(obj) {
for (key in obj) {
if (typeof obj[key] == 'object') {
traversIt(obj[key])
} else {
console.log(key + ': ' + obj[key]);
// do anything different you like with
// key or obj[key]
}
}
}
traversIt(x);
</script>
Copy linkTweet thisAlerts:
@ballmarkaauthorSep 04.2020 — @Sempervivum#1622943 wou, thank you very much for your help, it is perfect :-)
Copy linkTweet thisAlerts:
@ballmarkaauthorSep 04.2020 — I have second question. I need save information about dive of path in script beside of obj[key] (some coordinates).

For example: "condition: AND" --> x[0][1] it is possible too?
Copy linkTweet thisAlerts:
@SempervivumSep 04.2020 — Note that a numerical index like [0][1] is available only when traversing an array. If we handle an object it is not. Even the sequence of the entires is not defined reliably.
Copy linkTweet thisAlerts:
@ballmarkaauthorSep 04.2020 — ok, and I can separate each rules alone? Because condition "and" have one rule but condition "or" have 3 rules and I need condition "and" and "or " convert to other language and this is the reason why I need to have logical information that "and" belong that "price"=10.25 alongside that "or" have 3 conditions.
Copy linkTweet thisAlerts:
@ballmarkaauthorSep 04.2020 — it is there some way how to do?
Copy linkTweet thisAlerts:
@SempervivumSep 04.2020 — Unfortunately I do not yet understand the semantics of this structure. In the meantime I figured out this function:
function traversIt3(path, obj) {
if (typeof obj == 'object') {
if (Array.isArray(obj)) {
for (let i = 0; i < obj.length; i++) {
if (typeof obj[i] == 'object') {
traversIt3(path + '[' + i + ']', obj[i]);
} else {
console.log(path + '=' + obj[i]);
}
}
} else {
for (key in obj) {
if (typeof obj[key] == 'object') {
traversIt3(path + '[' + key + ']', obj[key]);
} else {
console.log(path + '[' + key + ']=' + obj[key]);
}
}
}
}
}
Check if you can use it, either "as is" or after modification.
Copy linkTweet thisAlerts:
@ballmarkaauthorSep 04.2020 — wou, it is interesting result from this code. It seems perfect. I am beginners in Javascript and I hope that I will be able to think like that after month :-) Thank you very much for your time :-)
Copy linkTweet thisAlerts:
@daveyerwinSep 04.2020 — @ballmarka#1622987 ...said

I need to have logical information that "and" belong that "price"=10.25 alongside that "or" have 3 conditions.

- - - - - - -



``<i>
</i> &lt;script&gt;
var x = {
"condition": "AND",
"rules": [
{
"id": "price",
"field": "price",
"type": "double",
"input": "number",
"operator": "less",
"value": 10.25
},
{
"condition": "OR",
"rules": [{
"id": "category",
"field": "category",
"type": "integer",
"input": "select",
"operator": "equal",
"value": 2
},
{
"id": "category",
"field": "category",
"type": "integer",
"input": "select",
"operator": "equal",
"value": 1
},
{
"id": "price",
"field": "price",
"type": "double",
"input": "number",
"operator": "equal",
"value": 10000
}
]
}
],

"valid": true
}
//var AND = 0;
// var OR = 1;
//alert(x.rules[AND].value)//alerts 10.25
//alert(x.rules[OR].rules.length)//alerts 3
console.log (x.condition, ' = ',x.rules[0],);
console.log (x.rules[1].condition, ' = ', x.rules[1].rules)
&lt;/script&gt;<i>
</i>
``
Copy linkTweet thisAlerts:
@ballmarkaauthorSep 08.2020 — Hi, I have other question. I need dynamicly use this function https://bodybuilder.js.org/ and I don´t know how to create. This function convert and create javascript code to elastic. For example If I have condition and I have to use "andQuery()" from bodybuilder(). Do you anybody know how to use this code from this site in javascript which include this bodybuilder() function?

Thank you for help
Copy linkTweet thisAlerts:
@VITSUSASep 09.2020 — @ballmarka#1623079 Have you get the solution of the existing problem? If yes, then you should ask the next question in the next thread.
×

Success!

Help @ballmarka spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 3.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...