简体   繁体   中英

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

You are probably looking for flat / flatMap :

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

Something like this perhaps?

    let newArray = findAll.map(object => object.items[0]});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM