在javascript中检索Array.map的结果

在javascript中检索Array.map的结果,javascript,Javascript,我有一个类似于这个结构的代码。这里似乎不需要异步函数,但就像我说的,它类似于我自己的代码,有点长 这是我的密码: const array = [ { methods: 'user.js'}, { methods: 'test.js'}, { methods: 'hello.js' }, ]; async function bigTest() { async function details() { const arr = []; const test = [];

我有一个类似于这个结构的代码。这里似乎不需要异步函数,但就像我说的,它类似于我自己的代码,有点长

这是我的密码:

const array = [
  { methods: 'user.js'},
  { methods: 'test.js'},
  { methods: 'hello.js' },
];
async function bigTest() {
  async function details() {
    const arr = [];
    const test = [];
    const files = array.map(async (i) => {
      arr.push({
        name: i,
        item: [
          {
            name: 'test',
          },
        ],
      });
      return test.push(arr);
    });
    return Promise.all(files);
  }
  const result = await details();
  console.log(JSON.stringify(result));
}
bigTest();
每次推入阵列的过程中,
console.log(arr)
都会为我返回:

[ { name: { methods: 'user.js', item: [] }, item: [ [Object] ] } ]

[ { name: { methods: 'user.js', item: [] }, item: [ [Object] ] },
  { name: { methods: 'test.js', item: [] }, item: [ [Object] ] } ]

[ { name: { methods: 'user.js', item: [] }, item: [ [Object] ] },
  { name: { methods: 'test.js', item: [] }, item: [ [Object] ] },
  { name: { methods: 'hello.js', item: [] }, item: [ [Object] ] } ] 
我想返回最后一个包含所有推送元素的数组,但结果是
[1,2,3]

当我在数组测试中执行console.log时,会得到重复的对象

返回test.push(arr)
不提供
arr
,而是提供
测试的新长度。您的
文件
将是整数的承诺,您的
结果
将是整数数组

此外,您还可以创建一个数组
arr
,以及一个数组
test
,您可以重复将
arr
推送到该数组。看起来您不需要任何嵌套,所以不要多次按下

您可以改为
returntest
returnarr
,但实际上您不应该自己推送这些值
map
已经为您创建了新数组,您只需要从回调返回相应的值:

async function bigTest() {
  const filePromises = array.map(async (i) => {
    // I assume you await something here, otherwise the `async` is really unnecessary
    return {
//  ^^^^^^
      name: i,
      item: [
        {
          name: 'test',
        },
      ],
    };
  });
  const files = await Promise.all(filePromises);
  console.log(JSON.stringify(files));
}
我想返回最后一个包含所有推送元素的数组,但结果是
[1,2,3]

发件人:

方法将一个或多个元素添加到数组的末尾,并返回数组的新长度

因此,您需要返回元素本身,而不是
test.push(arr)
的结果。更换
返回测试。按下(arr)

test.push(arr);
return test[test.length-1];
然后
console.log
只记录
result
中的最后一项:

console.log(JSON.stringify(result[result.length-1]));
const数组=[
{methods:'user.js',项:[]},
{方法:'test.js',项:[]},
{methods:'hello.js',项:[]},
];
异步函数bigTest(){
异步函数详细信息(){
常数arr=[];
常数测试=[];
const files=array.map(异步(i)=>{
arr.push({name:i,项:[{name:'test'}]});
测试推送(arr);
返回测试[测试长度-1];
});
返回承诺。所有(文件);
}
const result=等待详细信息();
log(JSON.stringify(result[result.length-1]);
}
bigTest()此行:

返回测试推送(arr)

返回
test
数组的新长度,即1、2、3,因此文件数组最终包含[1,2,3]

您可能要替换:

returnpromise.all(文件)

与:

returnpromise.all(文件)。然后(()=>arr)

它将执行承诺,然后返回arr数组的内容

或者,我不确定您想做什么,但您可以简化为:

async function bigTest() {
  async function details() {
    const files = array.map(async (i) => {
      return {
        name: i,
        item: [
          {
            name: 'test',
          },
        ],
      };
    });
    return Promise.all(files);
  }
  const result = await details();
  console.log(JSON.stringify(result));
}
bigTest();

旁注:为什么要使用
async
?您调用的不是异步API,所以为什么不在
.map
中删除所有
async
?您
返回test.push(arr)-这会不会使
文件
成为一个索引数组(即正向itegers)?我正在编写代码,我需要它们。我没有用一整段很长的代码问问题,而是用简单的代码做了一个与我类似的代码