0

I'm getting a group of objects in JSON like so

[ 
    { name: "Doc 1", 
      product: ["product 1", "product 2"],
      type: ["type 1", "type 2"] 
    },
    { name: "Doc 2", 
      product: ["product 1"],
      type: ["type 1"] 
    },
    { name: "Doc 3", 
      product: ["product 2"],
      type: ["type 2"] 
    }
    ...
]

I need to first clone the object for each product of the object (so I'd have 2 instances of Doc 1) and then I want to group the objects based on the product (that would give me another 2 objects) and then grouped based on the type which would give me another 2.

Initially we were just grouping based on the product but we then found our docs had multiple products associated with it and so we need to transform the original.

How would I clone each card to a new array for each product?

It's anticipated that the final group of objects would look like in this simple instance, Doc 1 is the only duplicate as it's associated with product 1 and 2.

[ 
  { name: "Doc 1", 
    product: ["product 1", "product 2"],
    type: ["type 1", "type 2"] 
  },
  { name: "Doc 1", 
    product: ["product 1", "product 2"],
    type: ["type 1", "type 2"] 
  },
  { name: "Doc 2", 
    product: ["product 1"],
    type: ["type 1"] 
  },
  { name: "Doc 3", 
    product: ["product 2"],
    type: ["type 2"] 
  }
  ...
]
Steven Grant
  • 1,236
  • 3
  • 15
  • 32
  • And your question is? – Rajesh May 30 '17 at 13:20
  • 1
    Can you add an example of how your final object looks ? – abhishekkannojia May 30 '17 at 13:22
  • JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. (And if that *were* JSON, it would be invalid JSON.) – T.J. Crowder May 30 '17 at 13:22
  • Possible duplicate of [What is the most efficient way to deep clone an object in JavaScript?](https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript) – Soren May 30 '17 at 13:48

1 Answers1

1

You can use Object.assign to clone the object and [].concat(yourArray) to clone an array of primitives.

var docs = [ 
    { name: "Doc 1", 
      product: ["product 1", "product 2"],
      type: ["type 1", "type 2"] 
    },
    { name: "Doc 2", 
      product: ["product 1"],
      type: ["type 1"] 
    },
    { name: "Doc 3", 
      product: ["product 2"],
      type: ["type 2"] 
    }
]

var cloned = docs.map(function (c) {
   // This will not clone the product
   c = Object.assign({}, c);
   
   // Clone the product array
   c.product = [].concat(c.product);
   return c;
});

// Group by products
var byProduct = {
  /*
    "product 1": [...]
  */
};

cloned.forEach(function (c) {
  c.product.forEach(function (prod) {
    var groupedDocs = byProduct[prod] = byProduct[prod] || [];
    groupedDocs.push(c);
  });
});

console.log(byProduct);
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474