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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:21:55+00:00 2026-06-18T00:21:55+00:00

If I have an array of objects in a var. I want to reduce

  • 0

If I have an array of objects in a var. I want to reduce this so that they are grouped by particular property. This is my code

array = tracks.reduce (x,y,i) ->
    x[y.album] = []
    x
, {}

albums = tracks.reduce (x,y,i) ->
    array[y.album].push {'name':y.name, 'mp3':y.mp3}
    array
, {}


console.log(albums)

It outputs what I want, however I want to know if there a better way to write this, without having to do the first loop, to create the empty arrays for the groups.

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-18T00:21:56+00:00Added an answer on June 18, 2026 at 12:21 am

    Yes, you can use the or= or ?= operator to assign array[y.ambum] only if it hasn’t been initialized; therefore using only one loop. BTW, i think it’s a bit confusing that the array variable is an object. Another way of coding this using a CoffeeScript loop instead of reduce is:

    albums = {}
    for {album, name, mp3} in tracks
      (albums[album] or= []).push {name, mp3}
    

    Notice that i’m using destructuring to get the track properties all at once.

    Or, if you want to use a reduce:

    albums = tracks.reduce (albums, {album, name, mp3}) ->
      (albums[album] or= []).push {name, mp3}
      albums
    , {}
    

    But i think the CS-loop version reads a bit better 🙂

    Bonus track (pun intended): if you happen to have Underscore.js, i’d strongly recommend to use groupBy, which does exactly this kind of grouping job:

    albums = _.groupBy tracks, (track) -> track.album
    

    Notice that the tracks for each album name in albums will be “complete” tracks (not just the name and mp3 properties).


    Update: A comment about performance: when asked to do something “efficiently” i interpret it as doing in the most direct and clean way possible (i’m thinking about the programmers’ efficiency when reading the code); but many will unequivocally relate efficiency to performance.

    About performance, all these three solutions are O(n), n being the number of tracks, in complexity; so neither is horribly worse than others.

    It seems raw for loops run faster on modern JS engines than their equivalent higher order brothers: forEach, reduce, etc (which is quite saddening IMO :(…). So the first version should run faster than the second one.

    In the case of the Underscore version, i won’t do any prediction, as Underscore is known for using the higher order functions a lot instead of raw for loops, but at the same time, that version does not create a new object for each track.

    In any case, you should always profile your code before changing your solution to one that might be more performant but is less readable. If you notice that that particular loop is a bottleneck, and you have a good set of data to benchmark it, jsPerf can be really useful 🙂

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

Sidebar

Related Questions

I have code similar to this filtering entries in an Array of Objects: var
I have a map array with objects stuffed with variables looking like this: var
i have this array of objects var ArrTdsObjects = []; function TdMaster(id, HeaderTxt, CurrentIndex,
I have an associative array of objects that I want to extend with a
I have an array of objects, like this: var companies = [ { name
I have an array that stores pointers to some objects and I want to
Say I have an array of objects that I want to be able to
Suppose I have an array of objects like this: var nodes = [{size:8,name:myName},{size:10,name:otherName},{size:11,name:userName}]; How
I have an array of objects. I want to find the maximum of this
I have a an array of objects that I want to enter into 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.