Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

数组分组条件汇总处理

源数据,三列分别表示 中心编号,受试者代码,受试结果

[
  ['CHN001', 'CHN001014', true ],
  ['CHN002', 'CHN002001', true ],
  ['CHN002', 'CHN002001', false ],
  ['CHN002', 'CHN002002', true ],
  ['CHN002', 'CHN002002', false ],
  ['CHN002', 'CHN002003', true ],
  ['CHN002', 'CHN002004', true ],
  ['CHN002', 'CHN002005', true ],
  ['CHN002', 'CHN002007', false ],
  ['CHN002', 'CHN002008', false ],
  ['CHN003', 'CHN003001', true ],
  ['CHN003', 'CHN003001', false ],
  ['CHN005', 'CHN005001', true ],
  ['CHN005', 'CHN005001', false ],
  ['CHN005', 'CHN005002', true ],
  ['CHN005', 'CHN005003', true ],
  ['CHN005', 'CHN005004', true ],
  ['CHN007', 'CHN007001', true ],
  ['CHN007', 'CHN007001', false ],
  ['CHN007', 'CHN007001', false ],
  ['CHN007', 'CHN007002', true ],
  ['CHN007', 'CHN007003', true ],
  ['CHN007', 'CHN007003', false ],
  ['CHN007', 'CHN007004', true ],
  ['CHN007', 'CHN007004', true ],
  ['CHN007', 'CHN007004', false ],
  ['CHN007', 'CHN007007', true ],
  ['CHN007', 'CHN007008', true ],
  ['CHN007', 'CHN007009', false ]
]

要求:
按照,中心编号和受试者代码分组,
比如 CHN005 下面包含了4个受试者,为CHN005001,CHN005002,CHN005003,CHN005004

  ['CHN005', 'CHN005001', true ],
  ['CHN005', 'CHN005001', false ],
  ['CHN005', 'CHN005002', true ],
  ['CHN005', 'CHN005003', true ],
  ['CHN005', 'CHN005004', true ],

CHN005001 下面又有两条记录,只有同时都是true,才按累加1处理

  ['CHN005', 'CHN005001', true ],
  ['CHN005', 'CHN005001', false ],

所以 CHN005001 为0
CHN005002,CHN005003,CHN005004都为1,最终

CHN005结果 为3

期望输出结果:

"CHN001"    "1"
"CHN002"    "3"
"CHN005"    "3"
"CHN007"    "4"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
var data = [
  ['CHN001', 'CHN001014', true ],
  ['CHN002', 'CHN002001', true ],
  ['CHN002', 'CHN002001', false ],
  ['CHN002', 'CHN002002', true ],
  ['CHN002', 'CHN002002', false ],
  ['CHN002', 'CHN002003', true ],
  ['CHN002', 'CHN002004', true ],
  ['CHN002', 'CHN002005', true ],
  ['CHN002', 'CHN002007', false ],
  ['CHN002', 'CHN002008', false ],
  ['CHN003', 'CHN003001', true ],
  ['CHN003', 'CHN003001', false ],
  ['CHN005', 'CHN005001', true ],
  ['CHN005', 'CHN005001', false ],
  ['CHN005', 'CHN005002', true ],
  ['CHN005', 'CHN005003', true ],
  ['CHN005', 'CHN005004', true ],
  ['CHN007', 'CHN007001', true ],
  ['CHN007', 'CHN007001', false ],
  ['CHN007', 'CHN007001', false ],
  ['CHN007', 'CHN007002', true ],
  ['CHN007', 'CHN007003', true ],
  ['CHN007', 'CHN007003', false ],
  ['CHN007', 'CHN007004', true ],
  ['CHN007', 'CHN007004', true ],
  ['CHN007', 'CHN007004', false ],
  ['CHN007', 'CHN007007', true ],
  ['CHN007', 'CHN007008', true ],
  ['CHN007', 'CHN007009', false ]
]

function computeScore(list) {
  const results = list.reduce((res, [no,code,result]) => {
    res[no] = res[no]||{};
    res[no][code] = (typeof res[no][code] == 'undefined' ? true : res[no][code])&&result;
    return res;
  }, {});
  
  return Object.keys(results).reduce((res,key) => {
    res[key] = Object.values(results[key]).reduce((acc,bol) => acc+bol,0)
    return res;
  }, {})
}
computeScore(data)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...