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

  • Home
  • SEARCH
  • 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 8826275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:05:47+00:00 2026-06-14T07:05:47+00:00

In JavaScript I am trying to convert an array of objects with similar keys:

  • 0

In JavaScript I am trying to convert an array of objects with similar keys:

[{'a':1,'b':2}, {'a':3,'b':4}, {'a':5,'b':6,'c':7}]

to an object with an array of values for each key:

{'a':[1,3,5], 'b':[2,4,6], 'c':[7]};

using underscore.js 1.4.2.

I have some working code below, but it feels longer and clunkier than just writing nested for loops.

Is there a more elegant way of doing this in underscore? Is there something simple I’m missing?

console.clear();

var input = [{'a':1,'b':2},{'a':3,'b':4},{'a':5,'b':6,'c':7}];
var expected = {'a':[1,3,5], 'b':[2,4,6], 'c':[7]};

// Ok, go
var output = _(input)
.chain()
// Get all object keys
.reduce(function(memo, obj) {
    return memo.concat(_.keys(obj));
}, [])
// Get distinct object keys
.uniq()
// Get object key, values
.map(function(key) {
    // Combine key value variables to an object  
    // ([key],[[value,value]]) -> {key: [value,value]}
    return _.object(key,[
        _(input)
        .chain()
        // Get this key's values
        .pluck(key)
        // Filter out undefined
        .compact()
        .value()
    ]);
})
// Flatten array of objects to a single object
// [{key1: [value]}, {key2, [values]}] -> {key1: [values], key2: [values]}
.reduce(function(memo, obj) {
    return _.extend(memo, obj);
}, {})
.value();

console.log(output);
console.log(expected);
console.log(_.isEqual(output, expected));

Thanks

  • 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-14T07:05:48+00:00Added an answer on June 14, 2026 at 7:05 am

    Sounds like you want zip for objects. This would be the analogous method for objects:

    _.transpose = function(array) {
        var keys = _.union.apply(_, _.map(array, _.keys)),
            result = {};
        for (var i=0, l=keys.length; i<l; i++) {
            var key = keys[i];
            result[key] = _.pluck(array, key);
        }
        return result;
    };
    

    However, I would just use

    _.transpose = function(array) {
        var result = {};
        for (var i=0, l=array.length; i<l)
            for (var prop in array[i]) 
                 if (prop in result)
                     result[prop].push(array[i][prop]);
                 else
                     result[prop] = [ array[i][prop] ];
        return result;
    };
    

    without any Underscore at all 🙂 Of course, you could use some iterator methods, it then might look like

    _.reduce(array, function(map, obj) {
        return _.reduce(obj, function(map, val, key) {
            if (key in map)
                map[key].push(val)
            else
                map[key] = [val];
            return map;
        }, map);
    }, {});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert a floating point to byte array/stream using javascript. My javascript
Possible Duplicate: Find object by id in array of javascript objects So I have
Background : I'm trying to convert some JavaScript code which uses the the Crossfilter
I am trying to convert some pre-existing html/JavaScript files to Flex. I tried making
Hi I am trying to post an array of javascript objects back to the
I'm trying to convert some code from Javascript to c. The function creates an
Possible Duplicate: Convert Javascript Array to JSON I am trying to figure out how
i m trying to convert php mysql_fetch_row result into javascript array so i write
I'm trying to pass a javascript Date object to a controller that I have
I am trying to convert a comma separated string into an array using 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.