Reputation: 89
I am working on a nested js application that is attempting to create a clean json schema - from more raw data.
So as I loop through the data - I want to pick up the label value which is nested like this.
label: data[x]["title"]["values"]["en"]
I want to create in the parent a pointer - to this data so
parentLabelPointer : "title.values.en"
but I can not simply do a get of this variable (using ember) like this
label: data[x][this.get("parentLabelPointer")]
if it was just one level - this would work. e.b. "parentLabelPointer : "title""
is there a clean way to pick this data up -- without having to try and drill down to the data in nested arrays like ["title"]["values"]["en"]
?
this is so I can make the module more modular -- if its to work with different data sets and different nesting levels.
my example
getLabel: function(prefix, pointer){
var trail = pointer.split(".");
var label = prefix;
trail.forEach(function(element) {
label = label[element];
});
return label;
}
usage
this.getLabel(data[x], this.get('parentLabelPointer'))
data[x] is like the known level of the branch -- but to find the label in the raw data --
parentLabelPointer - is like "title.values.en"
Upvotes: 0
Views: 42
Reputation: 29664
I think you want something like this:
import { computed } from '@ember/object';
...
label:computed('parentLabelPointer', 'data.[]', 'x', function(){
let x = this.get('x');
let data = this.get('data');
let parentLabelPointer = this.get('parentLabelPointer');
return data[x].get(parentLabelPointer);
}
That all said it's pretty unclear what x
, data
, etc is or how they're supposed to work, etc. This does feels like an XY problem
Upvotes: 1