Javascript recursion / Possible Babel Issue?
Are there any documented issues related to Javascript and recursion with Aurelia?
I am trying to take Aurelia on a test drive, by attempting to build some fairly complex custom components, one of which is a generic grid control supporting pagination, filtering, and grouping. In the process of attempting to implement the grouping capability, I'm using the Lodash framework to recursively iterate over an array of objects, producing a nested collection of objects grouped on 1..n property values from the original array.
I implemented a function on my view-model class to perform the grouping. The function looks like this:
// Takes object elements of an array and groups them based on
// one or more attribute values
// Source: https://gist.github.com/joyrexus/9837596
nest(seq, keys) {
if (!keys.length)
return seq;
var first = keys[0];
var rest = keys.slice(1);
return _.mapValues(_.groupBy(seq, first), function (value) {
return nest(value, rest);
});
}
When I attempt to invoke the nest function, I am seeing an error that indicates the attempt to recursively call itself fails. Yet, I know this bit of code works by itself, as the author provided a link to a JSFiddle implementation of it that clearly demonstrates that it works. The error I'm seeing when attempting to invoke this function within my view-model is:
ReferenceError: Can't find variable: nest
Are there any limitations related to recursive function calls associated with Aurelia, or perhaps Babel?
If nest is a method on a class you have to call it like this.nest. Just make the callback an arrow function.
I tried using this.nest, and got the following result.
TypeError: undefined is not an object (evaluating 'this.nest')
I'm just getting started with ES6.
You got me on the right track. This worked like a charm.
nest(seq, keys) {
if (!keys.length)
return seq;
var first = keys[0];
var rest = keys.slice(1);
return _.mapValues(_.groupBy(seq, first), (value) => {
return this.nest(value, rest);
});
}
Thank you very much!