How to fetch Object inside Array of Array? I am getting undefined
myArray = [
[{ name: test1, username: sampleuser1 }],
[{ name: test2, username: sampleuser2 }],
[{ name: test3, username: sampleuser3 }]
];
const forEachUser = await Promise.all(myArray[[0]].map(async (userSelected) =>
{
engagements.push(buildEmailPayload(bridgeInfo, userSelected, incident));
const callPayload = buildCallPayload(bridgeInfo, userSelected, incident);
})
this is my code
I’m trying to run the function in each of the username
3
Answers
You can get those user object by two methods:
1.Using index value
2.Using
Array.flat
method, this makes one level arrayTo fetch an object inside an array of arrays, you can use the index of the outer array and the index of the inner array to access the desired object. In the provided code,
myArray
is an array of arrays, where each inner array contains an object withname
andusername
Use array destructuring on the
userSelected
argument. The would give you the first element of anmyArray
‘s item:If you need to get
username
, add object destructuring: