0

I'm writing a complex recursive function which working with array of objects which includes each other.

For example, I have an object, like this one:

let item = {
   name: "Test",
   key: "value",
   pricing_methods: [ [Objects], [Objects] ] //array of object
}

where pricing_methods value is array of arrays, with array of objects inside:

[ 
   [
      { key: 'CONST', count: 1, reagent_items: [ {Object}, {Object} ] },
      { key: 'COMMDTY', count: 1, reagent_items: [ {Object}, {Object} ] }
   ],
   [
      { key: 'CONST', count: 2, reagent_items: [ {Object}, {Object} ] },
      { key: 'COMMDTY', count: 1, reagent_items: [ {Object}, {Object} ] },
      ...
   ],
   [
      ...
   ]
]

and as you may already guesses this reagent_items is array of objects/items from the beginning which also have it's own pricing_methods and so on...

If you have seen Christopher's Nolan «Inception» Movie it's some-kind like that.

THE PROBLEM

So as you may already saw I need to work with arrays of objects and it's properties. So here is my question:

What's the optimized or universal practice, npm module / library for such operations as remove/add/group/unwind an object inside array based on object's value?

Especially I needed group/unwind operations for object values inside array, how to achieve from:

   [
      { key: 'CONST', count: 1, reagent_items: [ {Object}, {Object} ] },
      { key: 'COMMDTY', count: 1, reagent_items: [ {Object}, {Object} ] }
   ]

this one:

[
   {
       name: "reagent_items#1",
       all_keys: "all_values#1",
       key: 'CONST',
   },
   {Object}, //all items for CONST and COMMDTY keys combined.
   {
       name: "reagent_items#2",
       all_keys: "all_values#2",
       key: 'COMMDTY',
   }
]

  • Should I define my own class for every item/pricing_method with getters/setters
  • Define my own array.method, like array.GroupByValue()
  • Or it's better to use lodash or _underscore or any other library which provides same methods like this?

P.S. I don't know all capabilities of lodash or any other array library so any advice will be helpful.

AlexZeDim
  • 3,520
  • 2
  • 28
  • 64
  • 1
    The questions you're asking require opinionated answers. Ramda, lodash, underscore and others all achieve similar goals using different techniques or design. In the end you will write the logic by yourself, with those libraries or without. That said, It's not recomended to overload prototype methods of objects (`Array.prototype.aaaa`). Something I've learned while programming, is that It's much easier writing logic (function) that deals with one object and then `map` the function over an array of similar objects. – MaxG Apr 17 '20 at 22:59
  • 1
    Thank you, for mentioning `Ramba` I haven't heard of it. Overriding `Array.prototype.aaaa` is *EXACTLY* what am I doing right now. So the question is about: should I continue this, or `write my own logic` via `lodash/_underscore/Ramda` etc? – AlexZeDim Apr 17 '20 at 23:00
  • 1
    If It's a one time project and no one is going to use what you write, then this is not a problem. If you're concerned with best practices and try to adhere to some software architecture paradigms, It's considered a bad practice since your extension method may interfere with other library logic or cause conflicts. – MaxG Apr 17 '20 at 23:12
  • 1
    Well, it's a «pet-project» in private repo, so it won't be a problem. Just to be sure, overwriting default `Array.prototype.method` doesn't affect performance and if I use `unique` names and won't face problems with other libs. – AlexZeDim Apr 17 '20 at 23:17
  • 1
    It's not a performance hit and I guess that if you are seeking better performance, you should try another language, like `Java` or `C`:D And about your other question: It's more than unique names as you can see in [this post](https://stackoverflow.com/questions/25481908/conflict-between-array-prototype-and-a-chain-select-box-plugin), but I guess these problems are rare... – MaxG Apr 18 '20 at 01:45

0 Answers0