There are 2 arrays being used.
This is the content of userGroups: console.log(this.items)
[
{
"id": 63,
"name": "URLGROUP-1643836551908"
}
]
The contents of urls are shown below:
userGroup can contain URLs
[
[
{
"id": 110,
"group_id": 63,
"url": "https://www.apple.com",
"url_num": 1,
"max_iteration": 2
},
{
"id": 111,
"group_id": 63,
"url": "https://www.google.com",
"url_num": 2,
"max_iteration": 2
}
]
]
The goal is to combine them in the following way: id, name, url
[
{
"id": 63,
"name": "URLGROUP-1643836551908",
"url": [
{
"id": 110,
"group_id": 63,
"url": "https://www.apple.com",
"url_num": 1,
"max_iteration": 2
},
{
"id": 111,
"group_id": 63,
"url": "https://www.google.com",
"url_num": 2,
"max_iteration": 2
}
]
}
]
Attempts to merge them have been unsuccessful so far. Performing nested loops could impact performance negatively.
What would be the best approach in this situation?
If I implement the following code snippet:
for (let i = 0; i < this.items.length; i++) {
for (let j = 0; j < urls.length; j++) {
if (i == j) {
this.items[i].urls = urls[j]
}
}
}
Here is how the updated items look like after executing the above code:
[
{
"id": 63,
"name": "URLGROUP-1643836551908",
"details": [
{
"id": 110,
"group_id": 63,
"url": "https://www.acme.com",
"url_num": 1,
"max_iteration": 2
}
]
}
]