Skip to content

Instantly share code, notes, and snippets.

@VegarRingdalAibel
Last active September 7, 2022 08:48
Show Gist options
  • Select an option

  • Save VegarRingdalAibel/a22415e0dbc5fc430945944eeb9d86cc to your computer and use it in GitHub Desktop.

Select an option

Save VegarRingdalAibel/a22415e0dbc5fc430945944eeb9d86cc to your computer and use it in GitHub Desktop.
sample
const currentrev = 1;
const prevRev = 0;
const data = [
{
tag: "tag1",
kg: 1,
rev: 1,
},
{
tag: "tag1",
kg: 2,
rev: 0,
},
];
const currentRevTags = [];
const currentRevRows = [];
const prevRevTags = [];
const prevRevRows = [];
data.forEach((row) => {
if (row.rev === currentrev) {
currentRevRows.push(row);
currentRevTags.push(row.tag);
}
if (row.rev === prevRev) {
prevRevRows.push(row);
prevRevTags.push(row.tag);
}
});
console.log(currentRevTags);
console.log(currentRevRows);
console.log(prevRevTags);
console.log(prevRevRows);
const changes = [];
currentRevTags.forEach((tag, i) => {
const index = prevRevTags.indexOf(tag);
if (index !== -1) {
const rowPrevRow = prevRevRows[index];
const rowCurrentRow = currentRevRows[i];
const keys = Object.keys(rowCurrentRow);
keys.forEach((key) => {
if (key !== "rev") {
if (rowPrevRow[key] !== rowCurrentRow[key]) {
changes.push({
tag,
key,
old: rowPrevRow[key],
new: rowCurrentRow[key],
});
}
}
});
}
});
console.log(changes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment