0

I am trying to copy an object, but I only want certain properties from the source object i.e. none of the read-only properties. I have tried let a = b, const b = Object.assign({}, a) etc.

Source Object is an array with the following properties: -

0:
accountId
customerId:
description:
image:
items: (4) [{…}, {…}, {…}, {…}]
productTitle:
selected:false
$exists:
$key:
__proto:

I only want the following properties in the new object:

0:
items: (4) [{…}, {…}, {…}, {…}]
productTitle:

Can anyone advise on the best way to do this?

Cerbrus
  • 60,471
  • 15
  • 115
  • 132
ccocker
  • 974
  • 3
  • 11
  • 35
  • 1
    `Object.assign` copies all enumerable properties to the target object. If you want to copy only a subset of them, do the assignments manually. – Bergi Oct 17 '17 at 14:54
  • You cannot exclude properties with `Object.assign`. Try this: `const b = {items: a.items, productTitle: a.productTitle };` – Mike Bovenlander Oct 17 '17 at 14:56
  • Great Thanks - this worked this.productGridData = []; this.products.forEach(product => { this.productGridData.push({ productTitle: product.productTitle, items: product.items }) }); – ccocker Oct 17 '17 at 15:06

1 Answers1

0

You could use a destructuring assignment of the object and then take only the parts.

let { item, productTitle } = object,
    newObject = { item, productTitle };
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324