[Solved] Transform an array to another array


As charlietfl said, in the future posts you must provide examples of code, that you’ve written to solve your problem.

Here’s the solution, it’s pretty simple:

const initialObject = {
  "items": [
    {
      "_id": "admin",
      "authorities": [
        {
          "name": "ROLE_ADMIN"
        }
      ]
    }, 
    {
      "_id": "user",
      "authorities": [
        {
          "name": "ROLE_USER"
        }
      ]
    }
  ]
}

const processedObject = _
  .map(initialObject.items, item => {
    return {
      _id: item._id,
      roles: _.map(item.authorities, a => a.name)
    }
  })

solved Transform an array to another array