-3

I'm still new to Javascript and trying to build a small project, I've done some research online about manipulating objects, but I mostly found forums about filtering arrays and not objects.

I have 2 objects:

let Object1 = {item1: {}, item2: {}, item3: {}}
let Object2 = {item4: {}, item5: {}}
const removeItem = 'item1'

I need to apply the following on these objects:

  1. Remove the item in "removeItem" from Object1 and return a new object:

    Object1 = {item2: {}, item3: {}}

  2. Copy item4 from Object2 to Object1 and return a new object:

    Object1 = {item2:(), item3: {}, item4: {}}

newuser1628
  • 109
  • 9

1 Answers1

0
  1. Remove the item in "removeItem" from Object1 and return a new object:
const newObject = { ...Object1 };
delete newObject[removeItem];
  1. Copy item4 from Object2 to Object1 and return a new object:
const newObject = { ...Object1, item4: Object2['item4'] };
Cerbrus
  • 70,800
  • 18
  • 132
  • 147