0

I have an array which consists of an array objects as shown:

dataArr = [
    {
       id: 1,
       arrObj: [
          {
             id: 11,
             label: 'apple'
          },
          {
             id: 12,
             label: 'ball'
          }
       ]
    },
    {
       id: 2,
       arrObj: [
          {
             id: 21,
             label: 'car'
          },
          {
             id: 22,
             label: 'dog'
          }
       ]
    }
];

I need to extract an array consisting of only arrObj objects:

var newArr = [
    {
         id: 11,
         label: 'apple'
      },
      {
         id: 12,
         label: 'ball'
      },
      {
         id: 21,
         label: 'car'
      },
      {
         id: 22,
         label: 'dog'
      }
];

Tried using reduce method unsuccessfully:

 dataArr.reduce((previousValue, currentValue, currentIndex, array) => {
     return previousValue. arrObj.concat(currentValue.arrObj)
 });

Let me know how to do this. Thanks

4 Answers 4

6

let dataArr = [
    {
       id: 1,
       arrObj: [
          {
             id: 11,
             label: 'apple'
          },
          {
             id: 12,
             label: 'ball'
          }
       ]
    },
    {
       id: 2,
       arrObj: [
          {
             id: 21,
             label: 'car'
          },
          {
             id: 22,
             label: 'dog'
          }
       ]
    }
];

let result = dataArr.flatMap(e => e.arrObj)
console.log(result)

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

Comments

2

You were pretty close.

  1. There's no arrObj property in your result, it's just an array.
  2. You need to provide an empty array as the initial value argument to reduce().

const dataArr = [{
    id: 1,
    arrObj: [{
        id: 11,
        label: 'apple'
      },
      {
        id: 12,
        label: 'ball'
      }
    ]
  },
  {
    id: 2,
    arrObj: [{
        id: 21,
        label: 'car'
      },
      {
        id: 22,
        label: 'dog'
      }
    ]
  }
];

const newArr = dataArr.reduce((previousValue, currentValue) => {
  return previousValue.concat(currentValue.arrObj)
}, []);

console.log(newArr);

1 Comment

The flatMap solution is probably better.
0

You could use in one line by using Spread Operator...

dataArr.reduce((previousValue, currentValue) => [...previousValue?.arrObj, ...currentValue?.arrObj]);

Tip: use Optional chaining ?. in case there is no property arrObj!

Comments

0

You can use the Array#reduce method as follows:

let dataArr = [
    {
       id: 1,
       arrObj: [
          {
             id: 11,
             label: 'apple'
          },
          {
             id: 12,
             label: 'ball'
          }
       ]
    },
    {
       id: 2,
       arrObj: [
          {
             id: 21,
             label: 'car'
          },
          {
             id: 22,
             label: 'dog'
          }
       ]
    }
];

let newArr = dataArr.reduce((acc,cur) => [...acc, ...cur.arrObj], []);

console.log( newArr );

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.