skip to Main Content

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


  1. You can get those user object by two methods:

    let myArray = [
       [{ name: "test1", username: "sampleuser1" }],
       [{ name: "test2", username: "sampleuser2" }],
       [{ name: "test3", username: "sampleuser3" }]
    ];
    

    1.Using index value

    const forEachUser = await Promise.all(
    myArray.map(userSelected => {
        engagements.push(buildEmailPayload(bridgeInfo, userSelected[0], incident));
        return buildCallPayload(bridgeInfo, userSelected[0], incident);
     })
    );
    

    2.Using Array.flat method, this makes one level array

    const forEachUser = await Promise.all(
    myArray[[0]].flat().map(userSelected => {
        engagements.push(buildEmailPayload(bridgeInfo, userSelected, incident));
        const callPayload = buildCallPayload(bridgeInfo, userSelected, incident);
        return callPayload
     })
    );
    

    Promise.all needs array of promises you need to return some promise

    Login or Signup to reply.
  2. To 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 with name and username

    myArray = [
      [{ name: 'test1', username: 'sampleuser1' }],
      [{ name: 'test2', username: 'sampleuser2' }],
      [{ name: 'test3', username: 'sampleuser3' }]
      
      ];
    
    for (let i = 0; i < myArray.length; i++) {
      for (let j = 0; j < myArray[i].length; j++) {
        const object = myArray[i][j];
        ...
      }
    }
    
    Login or Signup to reply.
  3. Use array destructuring on the userSelected argument. The would give you the first element of an myArray‘s item:

        const forEachUser = await Promise.all(myArray.map(async ([userSelected]) => 
        {
            engagements.push(buildEmailPayload(bridgeInfo, userSelected, incident));
            const callPayload = buildCallPayload(bridgeInfo, userSelected, incident);
    
        })
    

    If you need to get username, add object destructuring:

        const forEachUser = await Promise.all(myArray.map(async ([{username}]) => 
        {
            engagements.push(buildEmailPayload(bridgeInfo, username, incident));
            const callPayload = buildCallPayload(bridgeInfo, username, incident);
    
        })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search