How to merge two JSON arrays using jq

I have two json arrays that I'd like to merge with jq in the following way:

for each element in arr1
  add element to result
  if its id is found in arr2
    add the data array from that element in arr2 to the element in res

I have tried all kinds of select(), map(), piping etc., but I can't find the right combination.

The core command would probably look like this:

jq --argjson arr2 "$arr2" ' ... ' <<< $arr1

with ... being the filter in question. Thus I need to act on every element of arr1, add it to the output, search/filter in arr2 and if its not null, add the "data" field to the output.

Here are some data to try:

arr1='
[
  { "id" : "5",
    "name" : "set5"
  },
  { "id" : "1",
    "name" : "set1"
  },
  { "id" : "3",
    "name" : "set3"
  }
]
'
arr2='
[
  { "id" : "3",
    "data" : [
      {
        "foo" : "foo of 3",
        "foo1": "foo1 of 3"
      }
    ]
  },
  { "id" : "5",
    "data" : [
      {
        "foo" : "foo of 5",
        "foo1": "foo1 of 5"
      }
    ]
  }

]
'
# required merge output:

res='
[
  { "id" : "5",
    "name" : "set5",    
    "data" : [
      {
        "foo" : "foo of 5",
        "foo1": "foo1 of 5"
      }
    ]
  },
  { "id" : "1",
    "name" : "set1"
  },
  { "id" : "3",
    "name" : "set3",
    "data" : [
      {
        "foo" : "foo of 3",
        "foo1": "foo1 of 3"
      }
    ]
  }
]
'
How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum