HomeAbout MePublicationsContact

Concatenate array of objects insided array of objects

By Suresh Kumar Mukhiya
Published in Frontend Cheetseat
October 26, 2021
1 min read

Problem:

Given an array of objects, we want to concatenate nested array of objects. Look at the example objects given below.

Input:

const data = {
  activePolicies: [
    {
      policy: 1,
      locations: [
        {
          address: 'address1',
          locationID: 1
        },
        {
          address: 'address2',
          locationID: 2
        }
      ]
    },
    {
      policy: 2,
      locations: [
        {
          address: 'address3',
          locationID: 3
        },
        {
          address: 'address4',
          locationID: 4
        }
      ]
    }
  ],
  inActivePolicies: [
    {
      policy: 3,
      locations: [
        {
          address: 'address5',
          locationID: 5
        },
        {
          address: 'address6',
          locationID: 6
        }
      ]
    },
    {
      policy: 4,
      locations: [
        {
          address: 'address7',
          locationID: 7
        },
        {
          address: 'address8',
          locationID: 8
        }
      ]
    }
  ]
}

Expected Output:

const results = [
  {
    address: 'address1',
    locationID: 1
  },
  {
    address: 'address2',
    locationID: 2
  },
  {
    address: 'address3',
    locationID: 3
  },
  {
    address: 'address4',
    locationID: 4
  },
  {
    address: 'address5',
    locationID: 5
  },
  {
    address: 'address6',
    locationID: 6
  },
  {
    address: 'address7',
    locationID: 7
  },
  {
    address: 'address8',
    locationID: 8
  }
]

Solution 1:

function method1() {
  const policies = _.concat(data.inActivePolicies, data.activePolicies)

  const results = _.reduce(
    policies,
    (acc, item) => {
      return _.concat(acc, item.locations)
    },
    []
  )

  return results
}

Solution 2: (Fastest solution)

I have created a benchmark here to test the fastest solution. Hope the solution is still active when you are reading the blog.

function method2() {
  let newArray = []

  data.activePolicies.forEach(item => {
    item.locations.forEach(loc => {
      newArray.push(loc)
    })
  })

  data.inActivePolicies.forEach(item => {
    item.locations.forEach(loc => {
      newArray.push(loc)
    })
  })

  return newArray
}

Solution 3:

function method3() {
  let newArray = []

  for (const inAct of data.activePolicies) {
    for (const inActInner of inAct.locations) {
      newArray.push(inActInner)
    }
  }

  for (const inAct of data.inActivePolicies) {
    for (const inActInner of inAct.locations) {
      newArray.push(inActInner)
    }
  }

  return newArray
}

Tags

arrayJavaScript
Previous Article
Group Array of Objects by Key
Suresh Kumar Mukhiya

Suresh Kumar Mukhiya

Software Engineer

Topics

Artificial Intelligence
Frontend Cheetseat
My Philosophy
Paper Review
Research Methods
Software Architecture

Related Posts

Fastest way to check if an object is empty
October 27, 2021
1 min
© 2021, All Rights Reserved.

Quick Links

PublicationsAbout meContact Me

Social Media