-2

I am trying to query a JSON object using underscore library but I am unable to query the below JSON structure. '$scope.conScopeFreqStartDates' variable has the below JSON value.

I am passing 'frequencyCodeInput' value has 'Y' in the underscore script.

    [
       {
          "consolidationScopeId": 4008,
          "consolidationScopeCode": "S",
          "consolidationScopeLabel": "Individual",
          "frequencies": [
             {
                "frequencyCode": "M",
                "frequencyLabel": "Monthly",
                "startDates": [
                   "2016-01-31",
                   "2016-02-28"
                ]
             },
             {
                "frequencyCode": "Y",
                "frequencyLabel": "Annual",
                "startDates": [
                   "2016-12-31",
                   "2017-12-31"
                ]
             }
          ]
       }]

I am trying to get the startDates from the JSON object,

controller

var startDates = _.findWhere($scope.conScopeFreqStartDates, {
            'frequencies.frequencyCode': frequencyCodeInput
        }).startDates;

        $scope.startDates = startDates;

'startDates' is undefined for my above code.

SGN
  • 341
  • 1
  • 7
  • 23
  • Not sure exactly what you're asking here. You can query a JSON object like this: `object["key"]` Why do you need Underscore? – birdoftheday Feb 09 '16 at 16:35
  • he wants to get the value of startDates for the frequency that has a frequencyCode equal to frequencyCodeInput. [].filter() would do the job, underscore may have a method that might make it easier. – Kevin B Feb 09 '16 at 16:36
  • I am implementing something similar to this 'http://stackoverflow.com/questions/34883332/how-to-query-a-json-object' . But unable to query my JSOn structure which i posted above. – SGN Feb 09 '16 at 16:37

1 Answers1

1

I would call _.findWhere on the frequencies array on your object, not the entire object

var startDates = _.findWhere($scope.conScopeFreqStartDates.frequencies, {
            'frequencyCode': frequencyCodeInput
        }).startDates;

        $scope.startDates = startDates;
Scott Schwalbe
  • 430
  • 2
  • 4