Published
Edited
Jan 29, 2022
Insert cell
Insert cell
Insert cell
tableY2020Q4 = [
{ id1: "D", id2: "H", A: 0, B: 0, C: -4 },
{ id1: "E", id2: "I", A: 0, B: 0, C: 0 },
{ id1: "F", id2: "J", A: -8, B: 0, C: 1 },
{ id1: "G", id2: "K", A: 0, B: 9, C: 0 },
{ id1: "G", id2: "L", A: 0, B: 9, C: 0 }
]
Insert cell
Insert cell
tableY2021Q4 = [
{ id1: "D", id2: "H", A: 0, B: 0, C: 1 },
{ id1: "E", id2: "I", A: 0, B: 0, C: 0 },
{ id1: "F", id2: "J", A: 1, B: 0, C: 2 },
{ id1: "G", id2: "K", A: 0, B: 3, C: 0 },
{ id1: "G", id2: "L", A: 0, B: 3, C: 0 }
]
Insert cell
Insert cell
rowCount = tableY2020Q4.length
Insert cell
Insert cell
{
const result = [];
for (let index = 0; index < rowCount; index++) {
const resultRow = {
id1: tableY2020Q4[index].id2,
id2: tableY2020Q4[index].id2
};
if (tableY2020Q4[index].A === 0) {
resultRow.A = 0;
} else {
resultRow.A =
(tableY2020Q4[index].A - tableY2021Q4[index].A) / tableY2020Q4[index].A;
}
if (tableY2020Q4[index].B === 0) {
resultRow.B = 0;
} else {
resultRow.B =
(tableY2020Q4[index].B - tableY2021Q4[index].B) / tableY2020Q4[index].B;
}
if (tableY2020Q4[index].C === 0) {
resultRow.C = 0;
} else {
resultRow.C =
(tableY2020Q4[index].C - tableY2021Q4[index].C) / tableY2020Q4[index].C;
}
result.push(resultRow);
}
return result;
}
Insert cell
Insert cell
Insert cell
{
const columnNames = ["A", "B", "C"];
const resultTable = [];
for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
const resultRow = {
id1: tableY2020Q4[rowIndex].id2,
id2: tableY2020Q4[rowIndex].id2
};
for (const columnName of columnNames) {
if (tableY2020Q4[rowIndex][columnName] === 0) {
resultRow[columnName] = 0;
} else {
resultRow[columnName] =
(tableY2020Q4[rowIndex][columnName] -
tableY2021Q4[rowIndex][columnName]) /
tableY2020Q4[rowIndex][columnName];
}
}
resultTable.push(resultRow);
}
return resultTable;
}
Insert cell
Insert cell
calcPeriodOnPeriodChange = (table1, table2, columnNames) => {
const resultTable = [];
for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
const resultRow = { id1: table1[rowIndex].id1, id2: table1[rowIndex].id2 };
for (const columnName of columnNames) {
if (table1[rowIndex][columnName] === 0) resultRow[columnName] = 0;
else
resultRow[columnName] =
(table1[rowIndex][columnName] - table2[rowIndex][columnName]) /
table1[rowIndex][columnName];
}
resultTable.push(resultRow);
}
return resultTable;
}
Insert cell
Insert cell
calcPeriodOnPeriodChange(tableY2020Q4, tableY2021Q4, ["A", "B", "C"])
Insert cell
Insert cell
calcPeriodOnPeriodChangeUsingMap = (table1, table2, columnNames) => {
return table1.map((table1Row, rowIndex) => {
const resultRow = { id1: table1Row.id1, id2: table1Row.id2 };
columnNames.map((columnName) => {
if (table1Row[columnName] === 0) resultRow[columnName] = 0;
else
resultRow[columnName] =
(table1Row[columnName] - table2[rowIndex][columnName]) /
table1Row[columnName];
});
return resultRow;
});
}
Insert cell
calcPeriodOnPeriodChangeUsingMap(tableY2020Q4, tableY2021Q4, ["A", "B", "C"])
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more