As a JavaScript newcomer, I am seeking assistance with a particular task.
The array of objects in question looks like this:
const data =
{
"Total_packages": {
"package1": {
"tags": [
"kj21",
"j1",
"sj2",
"z1"
],
"expectedResponse": [
{
"firstName": "Name",
"lastName": "lastName",
"purchase": [
{
"title": "title",
"category": [
"a",
"b",
"c"
]
}
]
}
]
},
...
}
}
var arrRand = genNum(data, 3);
console.log(arrRand);
function genNum(data, loop='') {
...
}
}
To access the necessary values, I use expressions like arrRand[0].tag and arrRand[0].response.
However, duplicate responses are common in the output, which can be problematic.
[
]
My objective is to make API requests with a randomized "tags" value from the list above and then compare the response to the expected one.
Initially, I considered something like:
data.Total_packages.tags[Math.floor(Math.random() * data.Total_packages.tags.length)];
Yet, directly accessing "tags" without traversing through its parent, such as "package1" or "package2," makes it non-random.
I realize there must be a simpler solution that eludes me. Any guidance would be greatly appreciated.