Working with array of objects and it's values. npm module / libabry / classes

独自空忆成欢 提交于 2021-01-29 13:02:02

问题


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 optimise 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.

来源:https://stackoverflow.com/questions/61281510/working-with-array-of-objects-and-its-values-npm-module-libabry-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!