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"] 
  }
  ...
]
4
  • And your question is? Commented May 30, 2017 at 13:20
  • 1
    Can you add an example of how your final object looks ? Commented May 30, 2017 at 13:22
  • JSON is a textual notation for data exchange. (More here.) 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.) Commented May 30, 2017 at 13:22
  • Possible duplicate of What is the most efficient way to deep clone an object in JavaScript? Commented May 30, 2017 at 13:48

1 Answer 1

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);

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

Comments

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.