2

I am working on generating tax reports based off an array of orders. Essentially I need to convert the following array:

    [
  {
    "rate": 6.75,
    "code": "US-NC-Guilford-27409",
    "grand": 39.981625,
    "tax": 2.02
  },
  {
    "rate": 7.5,
    "code": "US-NC-Orange-27516",
    "grand": 186.25,
    "tax": 11.25
  },
  {
    "rate": 7.5,
    "code": "US-NC-Orange-27516",
    "grand": 29.19625,
    "tax": 1.5
  }
]

Into separate arrays of each class "code", where the classes "codes" could equal anything, and could have any number of that type. So it would look something like this:

[  
   US-NC-Guilford-27409:[  
      {  
         "rate":6.75,
         "code":"US-NC-Guilford-27409",
         "grand":39.981625,
         "tax":2.02
      }
   ],
   US-NC-Orange-27516:[  
      {  
         "rate":7.5,
         "code":"US-NC-Orange-27516",
         "grand":186.25,
         "tax":11.25
      },
      {  
         "rate":7.5,
         "code":"US-NC-Orange-27516",
         "grand":29.19625,
         "tax":1.5
      }
   ]
]

But I'm completely open to other ways of formatting the separated data, but for when the report is generated we have to give a log of orders from each tax class.

So how would you create that output using JavaScript (Node)?

2
  • 1
    That is not a valid structure that you are hoping to achieve. Perhaps you mean for the result to be an object, and not an array? Commented Jan 17, 2016 at 23:29
  • 1
    What did you try? What was the problem? Commented Jan 17, 2016 at 23:31

2 Answers 2

4

There is no need for a library, Array.prototype.reduce does the job:

var data =     [
  {
    "rate": 6.75,
    "code": "US-NC-Guilford-27409",
    "grand": 39.981625,
    "tax": 2.02
  },
  {
    "rate": 7.5,
    "code": "US-NC-Orange-27516",
    "grand": 186.25,
    "tax": 11.25
  },
  {
    "rate": 7.5,
    "code": "US-NC-Orange-27516",
    "grand": 29.19625,
    "tax": 1.5
  }
]

var codes = data.reduce(function(acc, obj) {
    if (!acc.hasOwnProperty(obj.code)) acc[obj.code] = [];
    acc[obj.code].push(obj);
    return acc;
}, {});

document.write(JSON.stringify(codes));

Edit

Based on Xotic750's suggestion, the hasOwnProperty test can be simplified to:

var codes = data.reduce(function(acc, obj) {
    if (!acc[obj.code]) acc[obj.code] = [];
    acc[obj.code].push(obj);
    return acc;
}, Object.create(null));

which can be further compressed to:

var codes = data.reduce(function(acc, obj) {
      return (acc[obj.code] || (acc[obj.code] = [])) && acc[obj.code].push(obj) && acc;
}, Object.create(null));

though I wouldn't suggest that for maintainable code. If you're into ES6 arrow functions, then:

var codes = data.reduce((acc, obj) => (acc[obj.code] || (acc[obj.code] = [])) && acc[obj.code].push(obj) && acc, Object.create(null));

does the job but is seriously obfuscated.

Sign up to request clarification or add additional context in comments.

3 Comments

As it is node and likely ES5 compliant, it may be safer to use Object.create(null) rather than {} to avoid any possible collisions. +1
@Xotic750—dunno about safer, but it would obviate the need for hasOwnProperty.
"safer" was probably not the best descriptive word, but you caught my drift ;)
-1

This looks like a perfect use for lodash _.groupBy() (see doc)

var processed = _.groupBy(original, 'code');

5 Comments

Perfect just what I was looking for!
Unless a tag for a framework or library is also included, a pure JavaScript answer is expected for questions with the [tag:JavaScript] tag.
Even when node was specifically mentioned? No one writes pure NodeJS.
I don't see what the node tag has to do with it. And of course people write in pure Javascript for it, how do you think that such libraries and frameworks exist?
I don't understand the downvotes. I think this answer is perfectly useful. If somebody wants to go ahead and code this without using any library, that's fine with me, but, why reinvent the wheel? Unless the original poster explicitly asks for a pure javascript and rules out any libraries, I believe this answer solves the problem and gives a heads up on a very useful library that might be used for similar problems

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.