Object Key Zipping?

Open
#182 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
48/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Stale
Tech stack
javascript
Domain
tooling

Research direction

Start with the existing zip entry point and compare its behavior with the before and after examples. Review the proposed zipObj behavior, add coverage for object keys and value arrays, and consider missing-value handling; done means the input object produces the demonstrated array of keyed objects.

Written by the indexing model from the issue text.

Description

after modules enhancement

I didn't find any other requests similar to this, so forgive me if there has been one already.

Underscore supplies a method called zip, which is great. What I'm wanting is the ability to zip objects by their keys.

var before = {
  key1: ['val1', 'val2', 'val3'],
  key2: ['val4', 'val5', 'val6'],
  key3: ['val7', 'val8', 'val9']
};

var after = [
  {  key1: "val1",  key2: "val4",  key3: "val7"  },
  {  key1: "val2",  key2: "val5",  key3: "val8"  },
  {  key1: "val3",  key2: "val6",  key3: "val9"  }
]

I have something working for this, is there any interest in adding functionality like this to the repo? I can submit a PR if needed, just wanted to gauge interest before I start adding tests and getting my code styled correctly.

My first pass at implementation:

var zipObj = function( obj ) {
  var keys = Object.keys(obj);
  return _.zip.apply(_, keys.map(function(k, i) { return obj[k] || '' }))
  .map(function(d) { 
    var obj = {};

    keys.forEach(function(k, i) { obj[k] = d[i] });
    return obj;
  });
}
Dominant language
JavaScript
Stars
622
Forks
114
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from documentcloud/underscore-contrib

All issues in documentcloud/underscore-contrib

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.