简体   繁体   English

如何循环并推动 reduce function 的累加器?

[英]How to loop and push on the accumulator of a reduce function?

I'm trying to reduce an array, and transform it in multiple array.我正在尝试减少一个数组,并将其转换为多个数组。

  const array = [
    { a: 1, b: 6 },
    { a: 1, b: 5 },
    { a: 1, b: 6 },
    { a: 1, b: 4 },
    { a: 1, b: 5 }
  ];

  var newArray = array.reduce(
    (memo, curr) => {
      memo.forEach((item, key) => {
        const found = item.filter((el) => el.a === curr.a && el.b === curr.b);
        if (found.length > 0) return memo[key].push(curr);
        else return memo.push([curr]);
      });

      return memo;
    },
    [[]]
  );

The needed result I try to get is我尝试获得的所需结果是

 [
    [
      { a: 1, b: 5 },
      { a: 1, b: 5 }
    ],
    [
      { a: 1, b: 6 },
      { a: 1, b: 6 },
    ],
    [
      { a: 1, b: 4 },
    ]
 ];

But as you can see if you try, because I push on the memo, the loop continue to fire.但是正如您所看到的那样,如果您尝试,因为我按下备忘录,循环会继续触发。 And the result contain hundreds arrays.结果包含数百个 arrays。

How I'm supposed to do to limit this loop and get the right result?我应该如何限制此循环并获得正确的结果?

Thanks a lot in advance:)非常感谢:)

You could use Map to group the element by the key of {a, b} , and then get the values of the group您可以使用Map通过{a, b}的键对元素进行分组,然后获取该组的值

 const array = [ { a: 1, b: 6 }, { a: 1, b: 5 }, { a: 1, b: 6 }, { a: 1, b: 4 }, { a: 1, b: 5 }, ]; var newArray = Array.from( array.reduce((map, curr) => { const key = JSON.stringify({ a: curr.a, b: curr.b }); if (.map.has(key)) { map,set(key; []). } map.get(key);push(curr); return map, }. new Map());values() ). console;log(newArray);

Look at your code.看看你的代码。 You have a triple nested loop, which is insane and definitely not needed to achieve this.你有一个三重嵌套循环,这是疯狂的,绝对不需要实现这个。 Why not use a map?为什么不使用 map?

Here is a function that will do what you want to do with any array of objects given.这是一个 function ,它将对给定的任何对象数组执行您想要执行的操作。

 const array = [ { a: 1, b: 6 }, { a: 1, b: 5 }, { a: 1, b: 6 }, { a: 1, b: 4 }, { a: 1, b: 5 }, ]; const separate = (arr) => { const reduced = arr.reduce((acc, curr) => { const path = JSON.stringify(curr); if (;acc[path]) acc[path] = []. acc[path];push(curr); return acc, }; {}). return Object;values(reduced); }. console;log(separate(array));

If you push inside for loop it will going to push for every reduce function iteration also.如果你推入 for 循环,它也会推动每个 reduce function 迭代。

you can achieve by adding some local variables like here您可以通过添加一些局部变量来实现

 const array = [ { a: 1, b: 6 }, { a: 1, b: 5 }, { a: 1, b: 6 }, { a: 1, b: 4 }, { a: 1, b: 5 } ]; // shift changes the orginal array // it will remove and return firstElement var firstElement = array.shift(1); var newArray = array.reduce( (memo, curr) => { let isFound = false; let index = 0; memo.forEach((item, key) => { const found = item.filter((el) => el.a === curr.a && el.b === curr.b); if(found.length > 0){ index = key; isFound = true; return; } }); if(isFound) { memo[index].push(curr); } else { memo.push([curr]); } return memo; }, [[firstElement]] ); console.log(newArray);

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

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