I need to search a large JSON file for an id
and return all the attributes. However, there is a parentId
attribute. This corresponds to the id
of another object whose attributes I need to include, and possibly another parentId
, etc.
m.getAttrs = function(o){
var a = []
// get the model based on id or object.id
if(o instanceof String)
var obj = json.models.filter(i => i.id == o)[0]
else
var obj = json.models.filter(i => i.id == o.id)[0]
////// On 2nd iteration: "TypeError: Cannot read property 'hasOwnProperty' of undefined" ///////////////
if( obj.hasOwnProperty("parentId") && obj.parentId != ""){
// get parent id
var pid = obj.parentId
// get parent attributes
a.push(m.getAttrs(pid)) // RECURSION //
}
// get attributes
var attrs = obj.attrids
// get Attribute Names
for(aid in attrs){
var aName = json.attrs.filter(i => i.id == aid)
a.push(aName)
}
return a
}
I'm seeing an error where obj
is not defined after the first iteration of getAttrs. I think this is because json.models.filter...
isn't finished, but it could be a lot of things.
I've tried a bunch of times to implement promises, but can't seem to get anything working, and it makes my code too messy for me to want to include it here.
How can I implement a promise to say "after you find the right model, CONTINUE (rather than execute another function)"?