简体   繁体   English

在javascript中检索Array.map的结果

[英]retrieve the result of Array.map in javascript

I have a similar code to this structure.我有一个与此结构类似的代码。 It seems as if I don't need the async functions here but like I said it's similar to my own code which is kind of long.似乎我在这里不需要异步函数,但就像我说的那样,它类似于我自己的代码,有点长。

Here is my code:这是我的代码:

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

The console.log(arr) returns for me each time the process of pushing into the array:每次推入数组的过程中, 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] ] } ] 

I want to return the last array with all the pushed element but what I get as a result is [1,2,3]我想返回包含所有推送元素的最后一个数组,但结果是[1,2,3]

and when I do a console.log on the array test, I get duplicate objects当我对阵列测试执行 console.log 时,我得到重复的对象

I want to return the last array with all the pushed element but what I get as a result is [1,2,3]我想返回包含所有推送元素的最后一个数组,但结果是[1,2,3]

From MDN :来自MDN

The push() method adds one or more elements to the end of an array and returns the new length of the array. push() 方法将一个或多个元素添加到数组的末尾并返回数组的新长度。

So you need to return the element itself and not the result of test.push(arr) .所以你需要返回元素本身而不是test.push(arr)的结果。 Replace return test.push(arr) by:return test.push(arr)替换为:

test.push(arr);
return test[test.length-1];

Then console.log only the last item in result :然后console.log只有result的最后一项:

console.log(JSON.stringify(result[result.length-1]));

 const array = [ { methods: 'user.js', item: [] }, { methods: 'test.js', item: [] }, { methods: 'hello.js', item: [] }, ]; async function bigTest() { async function details() { const arr = []; const test = []; const files = array.map(async (i) => { arr.push({ name: i, item: [{ name: 'test' }] }); test.push(arr); return test[test.length-1]; }); return Promise.all(files); } const result = await details(); console.log(JSON.stringify(result[result.length-1])); } bigTest();

I assume you are using arr and test for something else, otherwise, you could simplify your code a bit by getting rid of the intermediate arrays and logging the whole result array:我假设您正在使用arrtest其他内容,否则,您可以通过摆脱中间数组并记录整个结果数组来稍微简化代码:

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

return test.push(arr); does not give you the arr , it gives you the new length of test .不会给你arr ,它给你新的test长度。 Your files will be promises for integers, and your result will be an array of integers.您的files将是整数的承诺,您的result将是一个整数数组。

Additionally you create an array arr and also an array test to which you are repeatly pushing the arr .此外,您创建一个数组arr和一个数组test ,您将重复推送arr It doesn't seem like you want any nesting, so don't push multiple times.看起来您不需要任何嵌套,所以不要多次push

You could instead return test or return arr , but you really simply shouldn't be push ing those values yourself.您可以改为return testreturn arr ,但您真的不应该自己push这些值。 map already creates the new array for you, you just need to return the respective value from the callback: 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));
}

This line:这一行:

return test.push(arr);

is returning the new length of the test array, which is 1 then 2 then 3, so the files array ends up containing [1,2,3]正在返回test数组的新长度,即 1 然后 2 然后 3,因此文件数组最终包含 [1,2,3]

You probably want to replace:您可能想要替换:

return Promise.all(files);

with:和:

return Promise.all(files).then(() => arr);

which will execute the promises then return the contents of the arr array.这将执行承诺,然后返回 arr 数组的内容。

Alternatively, I'm not exactly sure what you're trying to do, but you may be able to simplify to:或者,我不确定您要做什么,但您可以简化为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM