👋

インプット値と予測値を比較して正誤表を作りたい

2020/12/14に公開

インプット値と予測値を比較して正誤表を作りたい

やりたいこと

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"]]

https://www.typescriptlang.org/play?#code/MYewdgzgLgBAlmADgV1gXhgbQN41AEwFMAuGARhgF8AaGXAkmAJitvpCNIGYqBdAKFCRYAQ2BRkIgDYwMOPB0YUadBZxgAWPv0FSRECDACKyQqYA8AFQB8dfjBiIATnABuIqIRj4PI0gEEnJxEATytbOV4AbntHZAgAC1kYAAo4TwBbUksASllbKAS4CAA6HygREpREtMycqJhYxBBEbJgAHxgRMBDklLy0AqLS8srEuAAzKH6YhwBzQlgpQjA5wv7SMGQMgCNCJ1UnReQnMBhC4rLfEuXVwqoYGModCeQwcThwPATCYABrfp2BxCaAwACOplMyTAhAA7sZIYRzFtdvtrDNYgB6bEwQCaDIBPBkAZgyAOwZAOMMgB+GQD9DIAkhggi1iCBQUBKExATgAomIErVCBl8uDEVV4tz0rySgwcvVYmIJNIWWzOcARZl+RCzIQhTVRRlxYpJbMFMIYBkRIhoXCYABZU0YhzYzEwQAgKhTKcTAHo6gHIDQCDDIByhkAwwyAeoZYoFgiEWU4QBkUiHQik1aYbis1gkciU-oQQhB+qnWRyuSlAYMgQ5DaCKk4FugBeqqi1bSWQbATVAlQBhRR9AD65crm22eyctE7UmKUACQVC5jefzAIFhYGsA1sw9HLIQ+ALhGWWS6PSXMC3YoYsgw3ZEFcWUpL8AmqWbbcUKR7i1ojNQeQAZB-jR4H0Qn+ela0DKkhSJKxbXkcEinD+iAlHS0zPlAtAAOSWv4litgAEihOSxA4zzXpMd6-gk7b-khr5IO+MBfjAACE95kY+lFdOIoHgdg+EllBJxnCacEIQBF7ITAKEAPIAGrsgASrh3GESWxEpIxpHkYQwlAfA1FQJ+35MepmkvmxspgXkXHXg4vEwQJ8GLEZokoQAMv4rYANLydeilWccMHYJQBqUHhwLgBAIDLDcIBzNGE5hhMEZRgJkr8M8Sq-AC9RAA

Discussion