-1

I have an array called data. How do i extract sub_data? Just need the sub_data part for each object.

const data = [
  {
    id: 1,
    title: 'Logo'
    sub_data: [
      {
        id: 2,
        title: 'Company Logo'
      },
      {
        id: 3,
        title: 'Website Logo'
      }, 
    ]
  },
  {
    id: 2,
    title: 'Brands'
    sub_data: [
      {
        id: 25,
        title: 'Company Brands'
      },
      {
        id: 3,
        title: 'Website Brands'
      }, 
    ]
  }
]

Example output will get two outputs because there is 2 objects:

const subData = [
      {
        id: 2,
        title: 'Company Logo'
      },
      {
        id: 3,
        title: 'Website Logo'
      }, 
]


const subData = [
       {
        id: 25,
        title: 'Company Brands'
      },
      {
        id: 3,
        title: 'Website Brands'
      },  
]

Not very sure how to use the map function just to get sub_data in the correct structure

3
  • 2
    data.find(element => element.id === yourId).sub_data Commented Jul 13, 2022 at 8:05
  • @RobertoZvjerković I'd use ?.sub_data in case of not found. Commented Jul 13, 2022 at 8:08
  • You can't have 2 outputs from one operation. By 2 outputs do you mean a nested array that contains your desired arrays? Commented Jul 13, 2022 at 8:13

3 Answers 3

2

You can use flatMap to get sub_data in one array

const data = [
  {
    id: 1,
    title: 'Logo',
    sub_data: [
      {
        id: 2,
        title: 'Company Logo'
      },
      {
        id: 3,
        title: 'Website Logo'
      }, 
    ]
  },
  {
    id: 2,
    title: 'Brands',
    sub_data: [
      {
        id: 25,
        title: 'Company Brands'
      },
      {
        id: 3,
        title: 'Website Brands'
      }, 
    ]
  }
]

const result = data.flatMap(item => item.sub_data)

console.log(result)

Sign up to request clarification or add additional context in comments.

2 Comments

I haven't been fast enough haha
haha, you and I have the same thought though. :D
1

If you want an array with the sub_data objects you can just map the original array:

const data = [
  {
    id: 1,
    title: 'Logo',
    'sub_data'
    : [
      {
        id: 2,
        title: 'Company Logo'
      },
      {
        id: 3,
        title: 'Website Logo'
      }, 
    ]
  },
  {
    id: 2,
    title: 'Brands',
    sub_data: [
      {
        id: 25,
        title: 'Company Brands'
      },
      {
        id: 3,
        title: 'Website Brands'
      }, 
    ]
  }
]

const mappedData = data.flatMap(obj => obj.sub_data)

console.log(mappedData)

Comments

1

Another solution would be to use the .forEach function of javascript.

const subData = [];
data.forEach(item => subData.push(...item.sub_data))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.