Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8366197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:51:22+00:00 2026-06-09T12:51:22+00:00

In my CouchDB reduce function I need to reduce a list of items to

  • 0

In my CouchDB reduce function I need to reduce a list of items to the unique ones.

Note: In that case it’s ok to have a list, it will be a small number of items of string type.

My current way is to set keys of a object, then return the keys of that object
since the place the code can’t use things like _.uniq for example.

I’d like to find a more elegant way to spell it than this.

function(keys, values, rereduce) {
  // values is a Array of Arrays
  values = Array.concat.apply(null, values);
  var uniq = {};
  values.forEach(function(item) { uniq[item] = true; });
  return Object.keys(uniq);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:51:24+00:00Added an answer on June 9, 2026 at 12:51 pm

    2021 answer:

    const unique = (arr) => [...new Set(arr)];
    unique([1, 2, 2, 3, 4, 4, 5, 1]); // [1, 2, 3, 4, 5]
    

    Here you just create a set from the given array and then convert it back to the array.
    I measured performance and it’s almost twice faster now than the approach proposed in the old answer I posted before. Also, it’s just a one-liner.

    Updated fiddle

    Old answer just for the record:

    Commonly, the approach you used is a good idea.
    But I could propose a solution that will make the algorithm a lot faster.

    function unique(arr) {
        var u = {}, a = [];
        for(var i = 0, l = arr.length; i < l; ++i){
            if(!u.hasOwnProperty(arr[i])) {
                a.push(arr[i]);
                u[arr[i]] = 1;
            }
        }
        return a;
    }
    

    As you can see we have only one loop here.

    I’ve made an example that is testing both your and my solutions. Try to play with it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an map function in a view in CouchDB that emits non-unique two
Each document of couchdb will have a new version after updating, and the previous
In CouchDB you always have to use map reduce to query results. In MongoDB
Rank amateur at both Map/Reduce and CouchDB here. I have a CouchDB populated with
Suppose I have couchdb docs that look like so: { _id: id, _rev: rev,
How do I version control my map/reduce functions for CouchDB? I'd like to be
Yep, I'm a SQL jockey (sorta) coming into the CouchDb Map/Reduce world. I thought
I am newbie to write couchdb map and reduce queries. One of my requirements
I am reading the O'Reilly CouchDB book. I am puzzled by the reduce/re-reduce/incremental-MapReduce part
CouchDB, version 0.10.0, using native erlang views. I have a simple document of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.