I'm trying to write a function that can be flexible in accepting a property value from an object, which may be a child of another property. In this example, each object in the myData array has a property named 'prop', which has a child property named 'subprop' that is a date value. I'm having trouble getting my function to find that value. The level of nesting could vary in other uses of the function. Here's where I'm at:
retrievedValues = getHistorical(myData, "prop.subprop", "Year");
function getHistorical(data, propertyChain, selectedHistory){
var returnedValues = [];
if(selectedHistory === 'Year'){
var dtCutoff = moment().subtract('years', 1);
}
for(var j=0, jj=data.length; j<jj; j+=1){
if(data[j][propertyChain] > dtCutoff){
returnedValues.push(data[j]);
}
}
return returnedValues;
}
I don't see a way to answer or end my question at this point, but I'll note that a solution that worked for me is a short recursive function noted here: stackoverflow.com/a/8817531/1689908