Creating nested arrays linked by value in JSObjects

I am trying to created nested arrays linked together by a key in JSObjects.
I have my first array and then the second inside the first. This works, but it returns all objects from the second array. I want to link the second array based on a key that they have in common, so it nest only the data related to the first array.

array1:MySQLQquery.data.map
(i =>
({
item1: i.name.valueOf(),
item2: i.place.valueOf()

array2:MySQLQquery2.data.filter.map
(i =>
({
item1: i.x.valueOf(),
item2: i.y.valueOf()
})
)
})
)

*The code that works for the second array is:
array2:MYSQLQuery2.data.filter(item=>item.x_id===1).map

This returns only 1 - but I cannot dynamically map the criteria ‘1’ to the previous array.

Hey Jyotish,

Jimmy from Appsmith support here. You should be able to give them separate names and match them like this:

array1: FirstQuery.data.map(item => {
  return {
    item1: item.name,
    item2: item.place,
    array2: SecondQuery.data.filter(anItem => anItem.x_id === item.id).map(anItem => {
      return {
        item1: anItem.x,
        item2: anItem.y
      }
    })
  }
})