1

I have an array of objects like below

myobjects = [
 {
   id: 1,
   images: [
    {imgName: 'image name.jpg',type: ''},
    {imgName: 'image name.jpg',type: ''}
 }
]

Now i assign myObj to another variable so that i can make changes to that variable without altering the myObj. e.g:

newObjects = [
 {
   id: 1
   images: [
    {imgName: "image name.jpg"},
    {imgName: "image name.jpg"}
   ]
  }
]
Musawer Shah
  • 314
  • 1
  • 9

1 Answers1

1

You can use map:

myobjects.map(({id, images}) => ({id, images: images.map(({imgName})=> ({imgName}))}));

An example:

let myobjects = [
  {
    id: 1,
    images: [
     {imgName: 'image name.jpg',type: ''},
     {imgName: 'image name.jpg',type: ''}
    ]
  }
 ]


const result = myobjects.map(({id, images}) => ({id, images: images.map(({imgName})=> ({imgName}))}));
console.log(result)
StepUp
  • 36,391
  • 15
  • 88
  • 148