From 4904c947c7e52a5440703cbe8997030249b17a19 Mon Sep 17 00:00:00 2001 From: kjj6198 Date: Mon, 9 Nov 2020 15:20:12 +0900 Subject: [PATCH] relation model to handle data --- src/models/relation.js | 82 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/src/models/relation.js b/src/models/relation.js index e69de29..63c7d72 100644 --- a/src/models/relation.js +++ b/src/models/relation.js @@ -0,0 +1,82 @@ +export default class Relation { + constructor(data) { + const keywords = ["年份", "年齡層"]; + const res = {}; + + data.forEach((d) => { + const result = {}; + Object.keys(d).forEach((key) => { + if (!keywords.includes(key)) { + result[key] = parseInt(d[key].replace(",", ""), 10); + } + }); + + Object.assign(res, { + [d["年齡層"]]: result, + }); + }); + + this.data = res; + } + + getTotalFor(relationship) { + let sum = 0; + + Object.keys(this.data).forEach((k) => (sum += this.data[k][relationship])); + + return sum; + } + + getProbabilityFor(name) { + let totalSum = 0; + let targetSum = 0; + + Object.keys(this.data).forEach((k) => { + totalSum += this.data[k]["總計"]; + targetSum += this.data[k][name]; + }); + + return targetSum / totalSum; + } + + // return + // [relation, number] + getRanking() { + const relations = [ + "配偶", + "前配偶", + "直系親屬", + "旁系親屬", + "家人的朋友", + "未婚夫/妻", + "男/女朋友", + "前男/女朋友", + "普通朋友", + "同事", + "同學", + "網友", + "客戶關係", + "上司/下屬", + "師生關係", + "鄰居", + "不認識", + ]; + + const ranking = {}; + relations.forEach((relation) => (ranking[relation] = 0)); + + Object.keys(this.data).forEach((k) => { + const data = this.data[k]; + + relations.forEach((relation) => { + ranking[relation] += data[relation]; + }); + }); + + return Object.keys(ranking) + .map((relation) => { + return [relation, ranking[relation]]; + }) + .sort((a, b) => b[1] - a[1]); + } +}