0

I have a big nested array of objects and each object has an object called 'set. Something like this:

const arr1 = [
  {
    id: 1,
    subject: 'Subject',
    set: {
      title: 'Title',
      subTitle1: 'SubTitle',
      subTitle2: 'SubTitle',
    },
  },
  {
    id: 2,
    subject: 'Subject',
    set: {
      title: 'Title',
      subTitle1: 'SubTitle',
      subTitle2: 'SubTitle',
    },
  },
]

I want to get the content of each 'set' object from 'arr1' into the 'arr2', like this:

const arr2 = [
  {
    title: 'Title',
    subTitle1: 'SubTitle',
    subTitle2: 'SubTitle',
  },
  {
    title: 'Title',
    subTitle1: 'SubTitle',
    subTitle2: 'SubTitle',
  },
]

1 Answer 1

1

You can use .map to extract the set from each object

const arr1 = [
  {
    id: 1,
    subject: 'Subject',
    set: {
      title: 'Title',
      subTitle1: 'SubTitle',
      subTitle2: 'SubTitle',
    },
  },
  {
    id: 2,
    subject: 'Subject',
    set: {
      title: 'Title',
      subTitle1: 'SubTitle',
      subTitle2: 'SubTitle',
    },
  },
]

const arr2 = arr1.map(a => a.set);

console.log(arr2 );

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

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.