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 7894475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:24:25+00:00 2026-06-03T07:24:25+00:00

Have data that has this kind of structure: $input = [ { animal: ‘cat’,

  • 0

Have data that has this kind of structure:

$input = [ { animal: 'cat', name: 'Rocky', value: 1 },
           { animal: 'cat', name: 'Spot',  value: 2 },
           { animal: 'dog', name: 'Spot',  value: 3 } ];

Need fastest possible method for converting to this format:

$output = { animal: [ 'cat', 'dog' ],
            name: [ 'Rocky', 'Spot' ],
            value: [ 1, 2, 3 ] };

The output should have keys equal to each of the keys in each object from the input. And the output values should be arrays with the sorted unique values. I found a few ways to do it using nested loops, but slower than I would like. With 30,000 elements to the input array with 8 keys for each of the objects, the best I have been able to do is 300ms in Chrome. Would like to get down to 100ms. Is there any faster method using a map or reduce?

  • 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-03T07:24:26+00:00Added an answer on June 3, 2026 at 7:24 am

    Here’s one way.

    $input = [ { animal: 'cat', name: 'Rocky', value: 1 },
               { animal: 'cat', name: 'Spot',  value: 2 },
               { animal: 'dog', name: 'Spot',  value: 3 } ];
    
    $output = {animal:{},name:{},value:{}};
    
    $input.forEach(function(v,i) { 
        $output.animal[v.animal] = 1;
        $output.name[v.name] = 1;
        $output.value[v.value] = 1;
    });
    
    $output.animal = Object.keys($output.animal);
    $output.name = Object.keys($output.name);
    $output.value = Object.keys($output.value);
    

    It prevents having to test each Array every time. You can performance compare to see if it helps.

    live example: http://jsfiddle.net/TJVtj/1/


    If you don’t want to hardcode the keys, you can make the solution generic.

    var keys = Object.keys($input[0]),
        $output = {};
    
    keys.forEach(function(v) {
        $output[v] = {};
    });
    
    $input.forEach(function(v) {
        keys.forEach(function(vv) {
            $output[vv][v[vv]] = 1;
        });
    });
    
    keys.forEach(function(v) {
        $output[v] = Object.keys($output[v]);
    });
    

    live example: http://jsfiddle.net/TJVtj/2/

    Warning. All the values will be strings since they’re fetched as object keys.

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

Sidebar

Related Questions

I have a varchar column that has data like this top<somenumber> so the word
I have a class that contains data from some model. This class has metadata
I have some data that has various attributes and I want to hierarchically group
I have a data access library that has a few classes that all implement
I have an application that uses Core Data that has a relationship that I
I have a php array that has a bunch of data that I need
I have an app that has a bunch of Core Data that it needs
I have a sql table of payroll data that has wage rates and effective
I have an entity in core data that has 2 Attributes. One that is
I have a data frame in R that has come about from running some

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.