Underscore js pluck nested key value

I have a JS object like this

var obj = {
    "2014" : {
        "11" : {
            "20" : {
                "Counts" : {
                    "c" : 2
                }
            },
            "21" : {
                "Counts" : {
                    "c" : 20
                }
            },
            "22" : {
                "Counts" : {
                    "c" : 20
                }
            }
        }
    },
    "_id" : "53d6883a2dc307560d000004",
    "createdate" :"2014-11-21T07:15:26.109Z"
};

      

As you can see, this is a structure that contains year> → month-> day-> counts-> c-> value structure I want to pluck the day and count the values ​​(c)

I have tried something like this

_.pluck(obj,'11')

      

but this is a good uptil only a month, and doesn't work for days like

_pluck(_.pluck(obj,'11'),'20') 

      

+3


source to share


3 answers


You can use a card like this. Pluck is an improved version of the map.

_.map(obj.2014.11, function (item) {
  return item.Counts.c
}

      



This will give you an array with all the c values ​​embedded in 11. But I wouldn't be surprised if I misunderstood your intentions ...

+14


source


Personally, I would create a date-bound data structure and then create views (very much like a couch structure) based on them:

var obj = {
"2014-11-20" : {
   "Counts" : {
      "c" : 2
   }
},
"2014-11-21" : {
   "Counts" : {
      "c" : 20
   }
},
"2014-11-22" : {
   "Counts" : {
      "c" : 20
   }
},
"_id" : "53d6883a2dc307560d000004",
"createdate" :"2014-11-21T07:15:26.109Z"
};

      

But given the existing structure, you just need to do the shorthand (actually several):



var allcounts = _.reduce(obj,function(result,item,key) {
  // key is now "2014", item is the value of {"11":....}
  _.reduce(item,function(result,item,key) {
    // key is now "11", item is the value of {"20":....}
    _.reduce(item,function(result,item,key) {
      // key is now the date, like "20", item is the value of {"Counts":{"c":2}}
      result.push(item.Counts.c);
    },result);
  },result);
},[]);

      

Terrible, but I can't think of a better way with such a deeply nested data structure

You can limit the range by working with key

var in the first, second, third _.reduce()

.

+1


source


You can do it:

var obj = {
                "2014" : {
                    "11" : {
                        "20" : {
                            "Counts" : {
                                "c" : 2
                            }
                        },
                        "21" : {
                            "Counts" : {
                                "c" : 20
                            }
                        },
                        "22" : {
                            "Counts" : {
                                "c" : 20
                            }
                        }
                    }
                },
                "_id" : "53d6883a2dc307560d000004",
                "createdate" :"2014-11-21T07:15:26.109Z"
            };


     for(x in obj){
        // console.log(obj[x]);
         for(y in obj[x]){
             for(z in obj[x][y]){
         console.log(obj[x][y][z]);// your output here
             }
         }
     }

      

but it will be helpful to create a date whenever possible for your implementation.

0


source







All Articles