使用spread减少es6丢失的嵌套对象数组

时间:2019-05-31 04:15:57

标签: javascript node.js ecmascript-6 lodash

按条件合并2组数据时出现问题。我已经调试了一个多小时,但不知道为什么。我在下面创建了一个示例。

我有这两个数据:

const staticRockData = {
    rockTypes: [
      {
        supplierRockTypes: [
          {
            rockCodes: ["1"],
            match_id: "abc"
          },
          {
            rockCodes: ["2"],
            match_id: "abc"
          }
        ]
      }
    ]
  };

  const gatewayRockData = {
    match_id: "abc",
    rocks: [{ rock_type: "1", rates: [] }, { rock_type: "2", rates: [] }]
  };

我有这个映射逻辑:

let rockTypes = staticRockData.rockTypes;

  rockTypes = rockTypes.reduce((accum, rockType) => {
    const matchedSourceId = rockType.supplierRockTypes.some(
      o2 => o2.match_id === gatewayRockData.match_id
    );
    if (matchedSourceId) {
      gatewayRockData.rocks.forEach(rock => {
        const matchRockType = rockType.supplierRockTypes.some(o2 => {
          return o2.rockCodes.includes(rock.rock_type);
        });

        if (matchRockType) {
          console.log("rock.rock_type", rock.rock_type);
          rockType = {
            ...rockType,
            rock_type: rock.rock_type,
            rates: rock.rates
          };
        }
      });
    }

    accum = [...accum, { ...omit(rockType, "supplierRockTypes") }];

    return accum;
  }, []);

  return {
    rocks: rockTypes
  };

我期望如此:

rocks: [
       {
         rates: [],
         rock_type: "1"
       },
       {
         rates: [],
         rock_type: "2"
       }
     ]
   }

当前解决方案缺少此信息:

{ rates: [], rock_type: "1"},我想知道我的错误在哪里。

省略是lodash的omit函数,但我不认为这是罪魁祸首。我在这里创建了一个演示:

https://codesandbox.io/s/condescending-platform-1jp9l?fontsize=14&previewwindow=tests

2 个答案:

答案 0 :(得分:2)

rockType被循环的每次重复覆盖:

rockType = {
        ...rockType,
        rock_type: rock.rock_type,
        rates: rock.rates
      };

您应通过以下方式将结果存储在另一个变量中:

...
let matchedRockTypes = [];
if (matchedSourceId) {
  gatewayRockData.rocks.forEach(rock => {
    const matchRockType = rockType.supplierRockTypes.some(o2 => {
      return o2.rockCodes.includes(rock.rock_type);
    });

    if (matchRockType) {
      console.log("rock.rock_type", rock.rock_type);
      matchedRockTypes.push({
        rock_type: rock.rock_type,
        rates: rock.rates
      });
    }
  });
}
return matchedRockTypes;
...

结果如下: https://codesandbox.io/s/tender-ritchie-i0qkt

答案 1 :(得分:1)

我不知道您的确切要求,但我找出了导致此问题的原因 问题出在您的减速器的forEach中,该版本已在以下版本中修复:

rockTypes = rockTypes.reduce((accum, rockType) => {
    const matchedSourceId = rockType.supplierRockTypes.some(
      o2 => o2.match_id === gatewayRockData.match_id
    );
    if (matchedSourceId) {
      gatewayRockData.rocks.forEach(rock => {
        const matchRockType = rockType.supplierRockTypes.some(o2 => {
          return o2.rockCodes.includes(rock.rock_type);
        });

        if (matchRockType) {
          accum = [...accum, { rock_type: rock.rock_type,
            rates: rock.rates }];;
        }
      });
    }

    return accum;
  }, []);

更具体地说,下面的forEach行:

if (matchRockType) {
          rockType = {
            ...rockType,
            rock_type: rock.rock_type,
            rates: rock.rates
          };
        }

每个时间循环都会运行一次,发现它与您匹配,因为它是对象而不是数组,所以它替换了rockType内容。

其他解决方案在forEach外部获取一个空数组,并在条件满足时将内容推入内部,并在返回时将其散布。

只是说您应该考虑重构代码。