Reputation: 4671
I'm learning CouchDb, And I have a problem
I have two documents
{
"_id": "2a8b8edb5d47c1e9fbafd414f60023af",
"_rev": "4-e305faba47bcb08569d225b181ba927e",
"skills": [
"js",
"php"
],
"type": "user",
"age": 32
}
{
"_id": "2a8b8edb5d47c1e9fbafd414f6002443",
"_rev": "4-edbfaa004143fc65df823f60b7f9ee94",
"skills": [
"js",
"nodejs",
"angular"
],
"type": "user",
"age": 28
}
I want to retrive an array like
['js','php','nodejs','angular']
without duplicate. I tried with this map
function(doc) {
var user = [];
for (item in doc.skills) {
user.push(doc.skills[item]);
}
emit( doc.type, user);
}
I got
["js", "php"]
["js", "nodejs", "angular"]
I tried with this reduce
function (key, skills){
var skill = [];
skill.push(skills);
return skill
}
and the result is
[[["js", "nodejs", "angular"], ["js", "php"]]]
Upvotes: 1
Views: 245
Reputation: 154504
In your reduce function, skills
is a list of lists of skills, so you need to concatenate each one onto the result:
function(key, skills) {
var result = [];
var seen = {};
skills.forEach(function(s) {
s.forEach(function(skill) {
if (seen[skill])
return;
seen[skill] = true;
result.push(skill);
});
});
return result;
}
Upvotes: 1