I have ruby background and am converting some ruby code to JavaScript.
I'm trying to understand the difference between hashes in ruby and JavaScript. I have a hash in ruby that looks like below:
main_hash = {"query" => { "filtered" => { "query"=> { "bool" => query_hash}}}}
I think the proper conversion for this in JavaScript would be basically a JSON like below:
var main_hash = {"query" : { "filtered" : { "query" : { "bool" : query_hash}}}}
But, I have some ruby logic that I'd like to replicate in JavaScript. And that code is:
if(query_hash.empty?)
main_hash["query"]["filtered"].delete("query")
else
main_hash["query"]["filtered"]["query"]["bool"] = query_hash
end
How do I access the nested attributes in JavaScript?
The reading I've been doing on w3schools indicates the below is the correct conversion, but I want to make sure I'm not missing any language semantics in this conversion. For example the delete only deletes the query object contained in filtered which is contained in query that is contained in main_hash correct?
if(!isEmptyObject(query_hash)){
delete main_hash.query.filtered.query;
} else {
main_hash.query.filtered.query.bool = query_hash;
}