Javascript 如何将解析对象推送到数组中?

Javascript 如何将解析对象推送到数组中?,javascript,arrays,asynchronous,parse-platform,Javascript,Arrays,Asynchronous,Parse Platform,我需要使用Parse平台中的Parse.UserobjectId数组来获取此用户,并保存一个名为“Project”的解析对象,其中包含一列“Gestor”,该列保存了作为项目经理的用户 这是我的阵列: data.gestor = [ "riid9e82uA", "tYxICE1ZOf", "MXYSc0iiYK" ] 这是我的职责: let gestors = []; await data.gestor.forEach(a

我需要使用Parse平台中的
Parse.User
objectId数组来获取此用户,并保存一个名为“Project”的解析对象,其中包含一列“Gestor”,该列保存了作为项目经理的用户

这是我的阵列:

data.gestor = [
  "riid9e82uA",
  "tYxICE1ZOf",
  "MXYSc0iiYK"
]
这是我的职责:

let gestors = [];
await data.gestor.forEach(async (value) => {
    console.log(value);
    let userQuery = new Parse.Query(Parse.User);
    userQuery.equalTo('objectId', value);
    let newGestor = await userQuery.first();
    console.log(JSON.stringify(newGestor, null, 2));
    await gestors.push(newGestor);
})
console.log(JSON.stringify(gestors, null, 2));
async function asyncForEach(array, callback) {
  for (let index = 0; index < array.length; index++) {
    await callback(array[index], index, array);
  }
}
在console输出上,我有Id,在代码首次将数组gestor打印为空之后,因此我将在console.log()上打印的对象打印到forEach中:

riid9e82uA
aw6cjLgajN
tYxICE1ZOf
[]
{
  "username": "teste123@email.com",
  "firstName": "ola",
  "lastName": "adeus",
  "emailVerified": false,
.
.
.
"tYxICE1ZOf": {
      "read": true,
      "write": true
    }
  },
  "objectId": "tYxICE1ZOf"
}

为什么代码首先执行第三个
console.log()
,然后执行第二个到forEach中,以及如何将对象推入数组,并在使用此数组后正确填充以创建新对象?

在forEach中使用async/await有点棘手

您可以创建自己的forEach函数:

let gestors = [];
await data.gestor.forEach(async (value) => {
    console.log(value);
    let userQuery = new Parse.Query(Parse.User);
    userQuery.equalTo('objectId', value);
    let newGestor = await userQuery.first();
    console.log(JSON.stringify(newGestor, null, 2));
    await gestors.push(newGestor);
})
console.log(JSON.stringify(gestors, null, 2));
async function asyncForEach(array, callback) {
  for (let index = 0; index < array.length; index++) {
    await callback(array[index], index, array);
  }
}
资料来源:


让我知道它是否有效

在forEach内部使用async/await有点棘手

您可以创建自己的forEach函数:

let gestors = [];
await data.gestor.forEach(async (value) => {
    console.log(value);
    let userQuery = new Parse.Query(Parse.User);
    userQuery.equalTo('objectId', value);
    let newGestor = await userQuery.first();
    console.log(JSON.stringify(newGestor, null, 2));
    await gestors.push(newGestor);
})
console.log(JSON.stringify(gestors, null, 2));
async function asyncForEach(array, callback) {
  for (let index = 0; index < array.length; index++) {
    await callback(array[index], index, array);
  }
}
资料来源:


让我知道它是否有效

使用
的答案对。。。像这样的

let gestors = [];
async function getGestors (gestores) {
    for (const gestor of gestores) {
       let userQuery = new Parse.Query(Parse.User);
       userQuery.equalTo('objectId', gestor);
       let newGestor = await userQuery.first();
       gestors.push(newGestor);
    }
}
await getGestors(data.gestor);
console.log(JSON.stringify(gestors, null, 2));
现在,我得到了预期的输出,并且在新对象上正确保存了Parse.User数组


我应该保留此主题还是删除此主题,因为另一个主题有解决方案?

在使用
for。。。像这样的

let gestors = [];
async function getGestors (gestores) {
    for (const gestor of gestores) {
       let userQuery = new Parse.Query(Parse.User);
       userQuery.equalTo('objectId', gestor);
       let newGestor = await userQuery.first();
       gestors.push(newGestor);
    }
}
await getGestors(data.gestor);
console.log(JSON.stringify(gestors, null, 2));
现在,我得到了预期的输出,并且在新对象上正确保存了Parse.User数组


我应该保留这个主题还是删除这个主题,因为另一个主题有解决方案?

是的,我解决了创建另一个函数来处理循环,但是使用了…就像我在这里的答案上显示的那样。是的,我解决了创建另一个函数来处理循环,但是使用了…就像我在这里的答案上显示的那样。