I've got a problem merging 2 sets of data by a condition. I've debugged for more than an hour but couldn't figure out why. I've created a sample below.
I have these 2 data:
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: [] }]
};
I have this mapping logic:
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
};
and I expected this:
rocks: [
{
rates: [],
rock_type: "1"
},
{
rates: [],
rock_type: "2"
}
]
}
The current solution is missing this:
{ rates: [], rock_type: "1"}
, I wonder where my mistake is.
Omit is lodash's omit
function, but I don't think that's the culprit. I created a demo here:
https://codesandbox.io/s/condescending-platform-1jp9l?fontsize=14&previewwindow=tests
omit
? If that's coming from a dependency, please include the import.