Skip to content

Underscore Object

Andrey Gershun edited this page Jan 18, 2016 · 2 revisions

_ Underscore Object

Source: StackOverflow

Question: How to reference the JSON object it self (to call a function on it)?

I have the following object and I would like to call it's function.

[{a:1, fn:function(){} }]

Now I know if the object would be nested ([{a:1, b:{fn:function(){} } }]) that I could do b->fn() but how do I do it when it's direct property of the first element?

Answer

Please use _ variable. This is a pseudo-variable, which includes the record whole itself:

SELECT _->fn() FROM ...

Here is the example:

var data = [
  {a:1, fn:function(){return 10}},
  {a:2, fn:function(){return 20}},
]

var res = alasql('SELECT _->fn() AS b FROM ?',[data]);

will returs:

[{"b":10},{"b":20}]

You can play with it in jsFiddle

Clone this wiki locally