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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:11:39+00:00 2026-06-05T08:11:39+00:00

I’m using underscore.js, of course any solution is fine, but I have an object

  • 0

I’m using underscore.js, of course any solution is fine, but I have an object that looks like this:

{ '4f871d4967e04': 
   [ { _id: 4f871d4adaf6fa492f000001,
       product_id: 4f871d43daf6fa4e2f000002,
       width: '300',
       height: 300,
       group: '4f871d4967e04' },
     { _id: 4f871d4adaf6fa492f000004,
       product_id: 4f871d43daf6fa4e2f000002,
       width: '150',
       height: 150,
       group: '4f871d4967e04' },
     { _id: 4f871d4bdaf6fa492f000007,
       product_id: 4f871d43daf6fa4e2f000002,
       width: '100',
       height: 100,
       group: '4f871d4967e04' },
     { _id: 4f871d4bdaf6fa492f00000a,
       product_id: 4f871d43daf6fa4e2f000002,
       width: '75',
       height: 75,
       group: '4f871d4967e04' } ],
  '4f871d51200de': 
   [ { _id: 4f871d51daf6faf42e000001,
       product_id: 4f871d43daf6fa4e2f000002,
       width: '300',
       height: 300,
       group: '4f871d51200de' },
     { _id: 4f871d52daf6faf42e000004,
       product_id: 4f871d43daf6fa4e2f000002,
       width: '150',
       height: 150,
       group: '4f871d51200de' },
     { _id: 4f871d53daf6faf42e000007,
       product_id: 4f871d43daf6fa4e2f000002,
       width: '100',
       height: 100,
       group: '4f871d51200de' },
     { _id: 4f871d53daf6faf42e00000a,
       product_id: 4f871d43daf6fa4e2f000002,
       width: '75',
       height: 75,
       group: '4f871d51200de' } ] }

I’d like to turn it to this:

{"4f871d4967e04" : {
 "300" : {
    _id : '',
  }
 "150" : {
    _id : '',
  }
 "100" : {
    _id : '',
  }
 "75" : {
    _id : '',
  }
}, "4f871d4967e04" : {
 "300" : {
    _id : '',
  }
 "150" : {
    _id : '',
  }
 "100" : {
    _id : '',
  }
 "75" : {
    _id : '',
  }
}

This way I can iterate through each “group” and simply use this["300"] to get the size that I want. I’m iterating through this in Handlebar’s templates, so I’m limited to what conditions I can check for.

Any suggestions would be really helpful.

Thank you!

  • 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-05T08:11:40+00:00Added an answer on June 5, 2026 at 8:11 am

    The obvious plain-jane JavaScript approach would probably be the quickest:

    var want = { };
    var k, i, o;
    for(k in data) {
        want[k] = { };
        for(i = 0; i < data[k].length; ++i) {
            o = data[k][i];
            want[k][o.width] = o;
        }
    }
    

    Simply loop through the main object (data) key by key and use an inner loop to rearrange the arrays into width→object mapping tables.

    Anything you come up with will probably be that nested loop in disguise. The Underscore version would look something like this:

    var want = _(data).reduce(function(outer, v, k) {
        outer[k] = _(v).reduce(function(inner, o) {
            inner[o.width] = o;
            return inner;
        }, { });
        return outer;
    }, { });
    

    That’s pretty much the original nested for loop set up with reduce in place of the loops. You’re pretty much limited to using reduce here as most Underscore functions really want to turn everything into an array. You could use each:

    var want = { };
    _(data).each(function(v, k) {
        var inner = { };
        _(v).each(function(o) {
            inner[o.width] = o;
        });
        want[k] = inner;
    });
    

    But that’s the same as the reduce approach with temporary variables. You could probably come up with other approaches but they’ll probably just be more variations on this theme.

    Demo: http://jsfiddle.net/ambiguous/ffcUC/2/


    If you want to send your data into a Handlebars template like:

    {{#each images}}<img src="../{{this.300._id}}" />{{/each}}
    

    Then you only want the values from what the above produces. But in this case, you can adjust your loops to save some work:

    var want = [ ];
    var k, i, o;
    for(k in data) {
        o = { };
        for(i = 0; i < data[k].length; ++i) {
            o[data[k][i].width] = data[k][i];
        }
        want.push(o);
    }
    return want;
    

    Or switch to reduce inside a map:

    var index_by_height = function(o) {
        return _(o).reduce(function(memo, i) {
            memo[i.height] = i;
            return memo;
        }, { });
    };
    return _.chain(data)
            .values()
            .map(index_by_height)
            .value();
    

    Demo: http://jsfiddle.net/ambiguous/tsx7z/

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words

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.