How to return array of objects from array of array with objects?

I have such array:

  let findAllSegmentProjectMediaFilesXref = [
{
  it: 0,
  bt: 0,
  items: [
    {
      mute: false,
      startPosition: 10,
      endPosition: 20,
      displayLength: 100,
      projectMediaFiles: {
        id: '123124'
      }
    }
  ]
},
{
  it: 9,
  bt: 10,
  items: [
    {
      mute: false,
      startPosition: 10,
      endPosition: 20,
      displayLength: 100,
      projectMediaFiles: {
        id: '1231231124'
      }
    }
  ]
},
{
  it: 2,
  bt: 2,
  items: [
    {
      mute: false,
      startPosition: 10,
      endPosition: 30,
      displayLength: 100,
      projectMediaFiles: {
        id: '123111124'
      }
    }
  ]
}

But i need to get array of objects like this:

    mute: elem.mute,
    startPosition: elem.startPosition,
    endPosition: elem.endPosition,
    displayLength: elem.displayLength,
    projectMediaFile: {
      //@ts-ignore
      id: elem.projectMediaFile.id,
    }

I tried to do something like this but it doesn’t work, can anybody help me?

    const arr =  findAllSegmentProjectMediaFilesXref?.map(x => x.segment.id === currentSegment?.id ? x.items.map(elem => {
  
    mute: elem.mute,
    startPosition: elem.startPosition,
    endPosition: elem.endPosition,
    displayLength: elem.displayLength,
    projectMediaFile: {
      //@ts-ignore
      id: elem.projectMediaFile.id,
    }
  
}) : null).filter(el => el !== null);

Answer

You are probably looking for flat / flatMap:

findAllSegmentProjectMediaFilesXref.map(e => e.items).flat()

Leave a Reply

Your email address will not be published. Required fields are marked *