简单的 reduce 函数不起作用,我在 couchDB 中的 reduce 函数代码有什么问题?
SImple reduce function is not working, what is wrong with my reduce function code in couchDB?
我是 couchDB 的新手。我从简单的 map/reduce 开始。我不知道为什么我无法收到我附加的代码段的正确值。提前致谢!!!
我的映射代码如下:
function(doc){
if(doc.release.formats.format.descriptions.description == 'Album'){
doc.release.artists.artist.forEach(function(i){emit(i.name,doc.release.title)})
}
}
我生成的地图值
键:"Trevor loveys" 值:"Era"
我的reduce代码
function(keys,values,rereduce)
{
for(var i=0; i<keys.length();i++)
{
if(keys[i] == "Trevor Loveys")
{
return values;
}
}
一旦我减少了,我得到的答案就像
键:"Trevor loveys" 值:空
更新。下面提供了我的 json 数据示例:
{
"release": {
"genres": {
"genre": "Electronic"
},
"status": "Accepted",
"videos": {
"video": [
{
"title": "Reflections- 2nd Nature",
"duration": 436,
"description": "Reflections- 2nd Nature",
"src": "http://www.youtube.com/watch?v=w4c8Xi56YZ8",
"embed": true
},
{
"title": "Trevor Loveys - Bra",
"duration": 414,
"description": "Trevor Loveys - Bra",
"src": "http://www.youtube.com/watch?v=DcakkW0uZcs",
"embed": true
},
{
"title": "Trevor Loveys - We can make it",
"duration": 463,
"description": "Trevor Loveys - We can make it",
"src": "http://www.youtube.com/watch?v=lJN7Wpl6HrE",
"embed": true
},
{
"title": "Trevor Loveys Forever After (ERA).mp4",
"duration": 311,
"description": "Trevor Loveys Forever After (ERA).mp4",
"src": "http://www.youtube.com/watch?v=00qmrYHgm54",
"embed": true
}
]
},
"labels": {
"label": {
"catno": "ALO CD003",
"name": "Alola"
}
},
"companies": "",
"styles": {
"style": [
"Deep House",
"Downtempo"
]
},
"formats": {
"format": {
"text": "",
"name": "CD",
"qty": 1,
"descriptions": {
"description": "Album"
}
}
},
"country": "UK",
"id": 1504,
"released": "1999-00-00",
"artists": {
"artist": [
{
"id": 2049,
"anv": "",
"name": "Trevor Loveys",
"role": "",
"tracks": "",
"join": "Presents"
},
{
"id": 8637,
"anv": "2nd Nature",
"name": "Second Nature",
"role": "",
"tracks": "",
"join": ""
}
]
},
"title": "Era",
"master_id": 11157,
"tracklist": {
"track": [
{
"position": 1,
"duration": "",
"title": "Forever After"
},
{
"position": 2,
"duration": "",
"title": "Wisdom"
},
{
"position": 3,
"duration": "",
"title": "Look To The Sky"
},
{
"position": 4,
"duration": "",
"title": "Falling Down"
},
{
"position": 5,
"duration": "",
"title": "Life Stories"
},
{
"position": 6,
"duration": "",
"title": "Rainy Afternoon"
},
{
"position": 7,
"duration": "",
"title": "Reflections"
},
{
"position": 8,
"duration": "",
"title": "Got To Go There"
},
{
"position": 9,
"duration": "",
"title": "We Can Make It"
},
{
"position": 10,
"duration": "",
"title": "No Compromise"
},
{
"position": 11,
"duration": "",
"title": "Era"
},
{
"position": 12,
"duration": "",
"title": "City 2 City"
},
{
"position": 13,
"duration": "",
"title": "Depth Charge"
}
]
},
"data_quality": "Correct",
"extraartists": {
"artist": [
{
"id": 49433,
"anv": "",
"name": "Chris Pedley",
"role": "Written-By, Producer",
"tracks": "",
"join": ""
},
{
"id": 2049,
"anv": "",
"name": "Trevor Loveys",
"role": "Written-By, Producer",
"tracks": "",
"join": ""
}
]
}
}
}
我的目的是获取艺术家的专辑名称"Trevor Loveys"
我用上面的方法达到了我的目的。但是,我无法理解为什么会失败。
reduce 函数中的键是一个数组,如:
[["some_key","89a85cad58c426cd85a55bac8e0350f8"]]
您可以在您的 reduce 函数中使用 log(key)
来测试它。
因此您的 if
块失败并且没有 reduce 被返回。
我是 couchDB 的新手。我从简单的 map/reduce 开始。我不知道为什么我无法收到我附加的代码段的正确值。提前致谢!!!
我的映射代码如下:
function(doc){
if(doc.release.formats.format.descriptions.description == 'Album'){
doc.release.artists.artist.forEach(function(i){emit(i.name,doc.release.title)})
}
}
我生成的地图值 键:"Trevor loveys" 值:"Era"
我的reduce代码
function(keys,values,rereduce)
{
for(var i=0; i<keys.length();i++)
{
if(keys[i] == "Trevor Loveys")
{
return values;
}
}
一旦我减少了,我得到的答案就像
键:"Trevor loveys" 值:空
更新。下面提供了我的 json 数据示例:
{
"release": {
"genres": {
"genre": "Electronic"
},
"status": "Accepted",
"videos": {
"video": [
{
"title": "Reflections- 2nd Nature",
"duration": 436,
"description": "Reflections- 2nd Nature",
"src": "http://www.youtube.com/watch?v=w4c8Xi56YZ8",
"embed": true
},
{
"title": "Trevor Loveys - Bra",
"duration": 414,
"description": "Trevor Loveys - Bra",
"src": "http://www.youtube.com/watch?v=DcakkW0uZcs",
"embed": true
},
{
"title": "Trevor Loveys - We can make it",
"duration": 463,
"description": "Trevor Loveys - We can make it",
"src": "http://www.youtube.com/watch?v=lJN7Wpl6HrE",
"embed": true
},
{
"title": "Trevor Loveys Forever After (ERA).mp4",
"duration": 311,
"description": "Trevor Loveys Forever After (ERA).mp4",
"src": "http://www.youtube.com/watch?v=00qmrYHgm54",
"embed": true
}
]
},
"labels": {
"label": {
"catno": "ALO CD003",
"name": "Alola"
}
},
"companies": "",
"styles": {
"style": [
"Deep House",
"Downtempo"
]
},
"formats": {
"format": {
"text": "",
"name": "CD",
"qty": 1,
"descriptions": {
"description": "Album"
}
}
},
"country": "UK",
"id": 1504,
"released": "1999-00-00",
"artists": {
"artist": [
{
"id": 2049,
"anv": "",
"name": "Trevor Loveys",
"role": "",
"tracks": "",
"join": "Presents"
},
{
"id": 8637,
"anv": "2nd Nature",
"name": "Second Nature",
"role": "",
"tracks": "",
"join": ""
}
]
},
"title": "Era",
"master_id": 11157,
"tracklist": {
"track": [
{
"position": 1,
"duration": "",
"title": "Forever After"
},
{
"position": 2,
"duration": "",
"title": "Wisdom"
},
{
"position": 3,
"duration": "",
"title": "Look To The Sky"
},
{
"position": 4,
"duration": "",
"title": "Falling Down"
},
{
"position": 5,
"duration": "",
"title": "Life Stories"
},
{
"position": 6,
"duration": "",
"title": "Rainy Afternoon"
},
{
"position": 7,
"duration": "",
"title": "Reflections"
},
{
"position": 8,
"duration": "",
"title": "Got To Go There"
},
{
"position": 9,
"duration": "",
"title": "We Can Make It"
},
{
"position": 10,
"duration": "",
"title": "No Compromise"
},
{
"position": 11,
"duration": "",
"title": "Era"
},
{
"position": 12,
"duration": "",
"title": "City 2 City"
},
{
"position": 13,
"duration": "",
"title": "Depth Charge"
}
]
},
"data_quality": "Correct",
"extraartists": {
"artist": [
{
"id": 49433,
"anv": "",
"name": "Chris Pedley",
"role": "Written-By, Producer",
"tracks": "",
"join": ""
},
{
"id": 2049,
"anv": "",
"name": "Trevor Loveys",
"role": "Written-By, Producer",
"tracks": "",
"join": ""
}
]
}
}
}
我的目的是获取艺术家的专辑名称"Trevor Loveys"
我用上面的方法达到了我的目的。但是,我无法理解为什么会失败。
reduce 函数中的键是一个数组,如:
[["some_key","89a85cad58c426cd85a55bac8e0350f8"]]
您可以在您的 reduce 函数中使用 log(key)
来测试它。
因此您的 if
块失败并且没有 reduce 被返回。