Is there a way to compare to values in two different array and create a new object?

First hello everyone, i will explain my problem and what i trigger to achive kind of to solve my issue.

I have to arrays that i need to compare, then create a new array of objects comparing the “group” id object, as you can see in my first array there is the array user which includes two objects , username Test-50 & Test-18, inside this objects they have groups array, which basically is necessary to compare with the second array.

First, i have two arrays:

the first one is:

   const user = [
      {
        username: "Test-50",
        group: [
          {
            id: 3127,
            name: "user default group",
          },
        ],
      },

      {
        username: "Test-18",
        group: [
          {
            id: 3069,
            name: "user default group",
          },
        ],
      },
    ];

My second array is:

Here we have the second array , here i need to compare the groups ids values from the first array with the actions scopes values to extract and use the localizedTexts.


    const actions = [
      {
        name: {
          localizedTexts: [
            {
              translation: "Enrichissement variété Google",
            },
          ],
        },
        scopes: [
          {
            group: {
              id: 3071,
              name: "Test-20",
            },
          },
          {
            group: {
              id: 3111,
              name: "Test-44",
            },
          },

          {
            group: {
              id: 3127,
              name: "Test-50",
            },
          },
        ],
      },
      {
        name: {
          localizedTexts: [
            {
              translation: "Filtre Hagberg",
            },
          ],
        },
        scopes: [
          {
            group: {
              id: 3127,
              name: "Test-50",
            },
          },
          {
            group: {
              id: 3069,
              name: "Test-18",
            },
          },
        ],
      },
    ];

My expected behaviour is :

    

expected result = [
      {
        username: "Test-50",
        group: [
          {
            id: 3127,
            name: "user default group",
          },
        ],

        actions: [
          {
            name: {
              localizedTexts: [
                {
                  translation: "Enrichissement variété Google",
                },
              ],
            },
          },

          {
            name: {
              localizedTexts: [
                {
                  translation: "Filtre Hagberg",
                },
              ],
            },
          },
        ],
      },

      {
        username: "Test-18",
        group: [
          {
            id: 3069,
            name: "user default group",
          },
        ],
        actions: [
          {
            name: {
              localizedTexts: [
                {
                  translation: "Filtre Hagberg",
                },
              ],
            },
          },
        ],
      },
    ];

I tried this function:

  const result = user.reduce((arr, e) => {
      arr.push(
        Object.assign(
          {},
          e,
          actions.find((x) =>
            x.scopes.find((w) => w.group?.id === e.groups[0]?.id)
          )
        )
      );
      return arr;
    }, []);

but the results it gives me is this one:

result = [

  {
    "username": "Test-50",
    "groups": [
      {
        "id": 3127,
        "name": "user default group"
      }
    ],
    "name": {
      "localizedTexts": [
        {
          "translation": "Enrichissement variété Google"
        }
      ]
    },
    "scopes": [
      {
        "group": {
          "id": 3071,
          "name": "Test-20"
        }
      },
      {
        "group": {
          "id": 3111,
          "name": "Test-44"
        }
      },
      {
        "group": {
          "id": 3127,
          "name": "Test-50"
        }
      }
    ]
  },
  {
    "username": "Test-18",
    "groups": [
      {
        "id": 3069,
        "name": "user default group"
      }
    ],
    "name": {
      "localizedTexts": [
        {
          "translation": "Filtre Hagberg"
        }
      ]
    },
    "scopes": [
      {
        "group": {
          "id": 3127,
          "name": "Test-50"
        }
      },
      {
        "group": {
          "id": 3069,
          "name": "Test-18"
        }
      }
    ]
  }
]

far away from what i need.

Thanks for your time.