0

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

Community
  • 1
  • 1
csupak
  • 145
  • 12
  • First of all, you cannot iterate through JavaScript objects by `length`. That only works for arrays with numeric indexes. – Pointy May 07 '14 at 14:17
  • If you want to be flexible, you should use a callback instead. – Bergi May 07 '14 at 14:17
  • @Pointy unless the object has a length property. :) – Jay Harris May 07 '14 at 14:19
  • @Pointy, the OP states that `myData` *is* an array: *each object in the myData array*. – Ayman Safadi May 07 '14 at 14:20
  • @AymanSafadi lots of PHP programmers think that JavaScript has associative arrays. – Pointy May 07 '14 at 14:23
  • When you say, *I'm having trouble getting my function to find that value*, what do you mean exactly? Have you tried debugging your script through a console? Are you getting an error message? A wrong value? I feel like we're missing something here... – Ayman Safadi May 07 '14 at 14:26
  • data[j][propertyChain] is undefined, when I want it to be the value of data[j].prop.subprop – csupak May 07 '14 at 14:53

1 Answers1

0

I made some tiny methods addressing this issue.

value = myData.getValueForKeyPath('prop.subprop');

https://github.com/eppz/eppz-js/blob/master/Classes/eppz!js/KeyPaths.js

Geri Borbás
  • 15,810
  • 18
  • 109
  • 172
  • thank you! trying this, I get TypeError ('undefined is not a function') on Object.prototype.addMethods (line 13). I copied your code to a .js file, and included it with – csupak May 07 '14 at 15:05
  • 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: http://stackoverflow.com/a/8817531/1689908 – csupak May 07 '14 at 15:28
  • Yap, that `addMethods` is a method copying function here: https://github.com/eppz/eppz-js/blob/master/Classes/eppz!js/Object.js. – Geri Borbás May 07 '14 at 16:15
  • Please note that underlying `defineProperty` browser coverage. http://kangax.github.io/es5-compat-table/#Object.defineProperty – Geri Borbás May 07 '14 at 16:20