I created a custom javascript function in the OrientDB Studio. The function takes a rid as an argument and performs a select query from that rid.
var graph = orient.getGraph();
var res = graph.command( "sql", "select outE('Rel').inV('B').size() as lower_num from " + rid );
So, the result of this query is a number. Depending on the value of this number the function should do different things. I wonder how to extract and use that value from the res variable inside the function. All my attempts have failed so far.. If I return the res variable from the function, the result looks like this:
[
{
"@type": "d",
"@rid": "#-2:1",
"@version": 0,
"lower_num": 2
}
]
So, I tried the following ( res[0]['lower_num'] == 2 ); ( res['lower_num'] == 2 )
and nothing worked.. They always returned false. Any ideas?
OrientDB put ODocument objects in the result set array. So try this:
return res[0].getRecord().field( "lower_num" );
var jsonhelp= [ { "@type": "d", "@rid": "#-2:1", "@version": 0, "lower_num": 2 } ];
var extractedNumber = jsonhelp[0].lower_num;
Now you can compare it with whatever you want; I hope it helps
like this you can extract any of the properties in JSON.
But in javascript there are two types of comparison
1) == simply compares values ,don't consider type
ex. '5' == 5 true , both are 5
5 == 5 true.
2)=== considers type too
ex. '5' === 5 is false, different type i.e String === integer
5 === 5 is true ,same type
来源:https://stackoverflow.com/questions/27657784/how-to-extract-the-result-of-a-query-from-json-inside-a-custom-javascript-functi