如何在数组的数组中获取对象

问题描述 投票:0回答:1

如何获取数组中的对象?我变得不确定


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);
            
        })

这是我的代码 我正在尝试在每个用户名中运行该函数

javascript arrays multidimensional-array mapping
1个回答
0
投票

您可以通过两种方法获取这些用户对象:

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

1.使用索引值

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

2.使用

Array.flat
方法,这使得一级数组

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
需要一系列承诺,你需要返回一些承诺

© www.soinside.com 2019 - 2024. All rights reserved.