javascript 下划线 js 提取嵌套键值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27057960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
underscore js pluck nested key value
提问by Brij Raj Singh - MSFT
I have a JS object like this one
我有一个像这样的 JS 对象
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 structure which contains year>->month->day->counts->c->value structure I want to pluck out the day and Count(c) values out of it
正如你所看到的,这是一个包含 year>->month->day->counts->c->value 结构的结构我想从中取出日期和 Count(c) 值
I tried something like this
我试过这样的事情
_.pluck(obj,'11')
but this is good uptil only month, and doesn't work for days like
但这只有一个月才有用,而且像这样的日子不起作用
_pluck(_.pluck(obj,'11'),'20')
回答by Anders ?stman
You can use map like this. Pluck is a refined version of map.
您可以像这样使用地图。Pluck 是地图的精制版。
_.map(obj.2014.11, function (item) {
return item.Counts.c
}
This will give you an array with all c values embedded in 11. But I would not be suprised if I have misunderstood your intent...
这将为您提供一个数组,其中所有 c 值都嵌入在 11 中。但如果我误解了您的意图,我不会感到惊讶......
回答by deitch
Personally, I would have created the data structure keyed by date, and then created views (this looks a lot 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 your existing structure, you may just need to do a reduce (actually multiple):
但是考虑到您现有的结构,您可能只需要做一个 reduce(实际上是多个):
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);
},[]);
Ugly, but I cannot think of a better way with this kind of deeply nested data structure
丑陋,但我想不出更好的方法来处理这种深度嵌套的数据结构
You can limit the range by working with the key
var in the first, second, third _.reduce()
.
您可以通过使用key
第一个、第二个、第三个中的var来限制范围_.reduce()
。
回答by Suchit kumar
you can do 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"
};
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 good to construct date wise if possible for your implementation.
但如果可能的话,最好为您的实施构建日期。