-2

Was not able to put entire question in the title. I have an array of objects and each object has two key values. first key value is a string and the second is another array of objects.

I need to iterate over this array so that I only have unique master names. if the name exists in new array already then only push the related array to the corresponding object. I hope this makes some kind of sense to who is reading this.

Example of array i am wanting to iterate over:

[
{master: 'Omni Mixer Homogenizer', related: [{_id: "59f5f1fe7d079b6be8fdef5c", id: 75, product_name: "product name"}]},
{master: 'Omni Mixer Homogenizer', related: [{_id: "59f5f1fe7d079b6be8fdef5e", id: 89, product_name: "product name"}]},
{master: 'μHB MICRO HOMOGENIZER', related: [{_id: "59f5f1fe7d079b6be8fdef5r", id: 102, product_name: "product name"}]},
{master: 'μHB MICRO HOMOGENIZER', related: [{_id: "59f5f1fe7d079b6be8fdef5f", id: 67, product_name: "product name"}]},
{master: 'Omni Mixer Homogenizer', related: [{_id: "59f5f1fe7d079b6be8fdef5z", id: 92, product_name: "product name"}]},
]

Based on the array above i want to get a result that looks like this:

[
{master: 'Omni Mixer Homogenizer', related: [{_id: "59f5f1fe7d079b6be8fdef5c", id: 75, product_name: "product name"}, {_id: "59f5f1fe7d079b6be8fdef5e", id: 89, product_name: "product name"},{_id: "59f5f1fe7d079b6be8fdef5z", id: 92, product_name: "product name"}]},
{master: 'μHB MICRO HOMOGENIZER', related: [{_id: "59f5f1fe7d079b6be8fdef5r", id: 102, product_name: "product name"}, {_id: "59f5f1fe7d079b6be8fdef5f", id: 67, product_name: "product name"}]},
]

Any help would be greatly appreciated, thank you.

Travis Michael Heller
  • 1,210
  • 3
  • 16
  • 34
  • Suggestion for better Title would be helpfull. – Travis Michael Heller Nov 09 '17 at 14:40
  • are you pushing the entire object to the array or just the name? – Robbie Milejczak Nov 09 '17 at 14:41
  • please add the code in text form. – Nina Scholz Nov 09 '17 at 14:41
  • I added some of the code i am working with above. – Travis Michael Heller Nov 09 '17 at 14:51
  • "the second is another array of objects" - which always has exactly one element? Seems that the code that delivers this array in the first place isn't quite right, rather than a band-aid of using javascript to properly format your array, whatever technology (php?) that is delivering the array in the first place should be fixed. – James Nov 09 '17 at 14:59
  • James, you are more than likely correct. I will be going back and refactoring my code but I am just trying to get a solution so I can better understand the process. I completely understand what you are saying though. I am actually in the middle of refactoring my angular app so i am only calling a http request once from my service instead of multiple times. – Travis Michael Heller Nov 09 '17 at 15:04
  • This is less of a question and more of a requirement. – Liam Nov 09 '17 at 15:16
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Liam Nov 09 '17 at 15:17

1 Answers1

2

you don't need underscore.js for this.

let objects = [
{master: 'Omni Mixer Homogenizer', related: [{_id: "59f5f1fe7d079b6be8fdef5c", id: 75, product_name: "product name"}]},
{master: 'Omni Mixer Homogenizer', related: [{_id: "59f5f1fe7d079b6be8fdef5e", id: 89, product_name: "product name"}]},
{master: 'μHB MICRO HOMOGENIZER', related: [{_id: "59f5f1fe7d079b6be8fdef5r", id: 102, product_name: "product name"}]},
{master: 'μHB MICRO HOMOGENIZER', related: [{_id: "59f5f1fe7d079b6be8fdef5f", id: 67, product_name: "product name"}]},
{master: 'Omni Mixer Homogenizer', related: [{_id: "59f5f1fe7d079b6be8fdef5z", id: 92, product_name: "product name"}]},
]

function process(object) {
  let results = [];
  objects.forEach(object => { // iterate through array
    let result = results.find(x => x.master === object.master); 
    if (result) { // if item with "master" exists 
      object.related.forEach(item => { // add all realted items to existing items "related" array
        if (!result.related.find(x => x._id === item._id)) {
          result.related.push(item);
        }
      });
    } else { // otherwise add an item to "results" array
      results.push(object);
    }
  });
  return results;
}

console.log(process(objects));