👋
インプット値と予測値を比較して正誤表を作りたい
インプット値と予測値を比較して正誤表を作りたい
やりたいこと
const input = [{ code: 1 }, { code: 2 }, { code: 3 }];
const actual = [{ code: 1 }, { code: 4 }];
check(input, actual);
[
{ code: 1, result: "MATCH" }, // 一致
{ code: 2, result: "OVER" }, // 過剰
{ code: 3, result: "OVER" }, // 過剰
{ code: 4, result: "LACK" }, // 不足
];
Code
const input = [{ code: 1 }, { code: 2 }, { code: 3 }];
const actual = [{ code: 1 }, { code: 4 }];
class Queue<T> {
private data: Array<T> = [];
push = (item: T) => this.data.push(item);
pop: T | any = () => this.data.shift();
get length(): number {
return this.data.length;
}
}
function check() {
const queue = new Queue<number>();
/// すべてのデータをset
input.forEach((item) => queue.push(item.code));
actual.forEach((item) => queue.push(item.code));
const map = new Map();
/// 各データの属性チェック
Array.from(Array(queue.length).keys()).forEach(() => {
const target = queue.pop();
const matchCode = (_target: number, _list: Array<unknown>) =>
_list.find((elem: any) => elem.code == _target);
if (matchCode(target, input) && matchCode(target, actual)) {
return map.set(target, "MATCH");
}
if (matchCode(target, input) && !matchCode(target, actual)) {
return map.set(target, "OVER");
}
if (!matchCode(target, input) && matchCode(target, actual)) {
return map.set(target, "LACK");
}
return {};
});
console.log(Array.from(map));
}
check();
// [LOG]: [[1, "MATCH"], [2, "OVER"], [3, "OVER"], [4, "LACK"]]
Discussion