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
311 views
in Technique[技术] by (71.8m points)

javascript - 如何根据通用键值减少对象数组?(How to reduce array of objects based on common key values?)

Let's say, I have an Array of object which looks like:(假设我有一个对象数组,如下所示:)

var jsonData = [ {"DS01":123,"DS02":88888,"DS03":1,"DS04":2,"DS05":3,"DS06":666}, {"DS01":123,"DS02":88888,"DS03":2,"DS04":3,"DS05":4,"DS06":666}, {"DS01":124,"DS02":99999,"DS03":3,"DS04":4,"DS05":5,"DS06":333}, {"DS01":124,"DS02":99999,"DS03":5,"DS04":6,"DS05":7,"DS06":333} ]; You can see there are some common key fields which are DS01, DS02 and DS06.(您会看到一些常见的键字段,分别是DS01,DS02和DS06。) Firstly, I want to find which are common group of keys .(首先,我想找出哪些是常见的键组 。) For first 2 Objects : DS01 = 123, DS02 = 88888, DS06 = 666(对于前两个对象: DS01 = 123,DS02 = 88888,DS06 = 666) For last 2 Objects : DS01 = 124, DS02 = 99999, DS06 = 333(最后2个对象: DS01 = 124,DS02 = 99999,DS06 = 333) I want to convert this array of objects to a format like this:(我想将此对象数组转换为如下格式:) var jsonDataReduced = [{ "DS01": 123, "DS02": 88888, "DS03": [1, 2], "DS04": [2, 3], "DS05": [3, 4], "DS06": 666 }, { "DS01": 124, "DS02": 99999, "DS03": [3, 5], "DS04": [4, 6], "DS05": [5, 7], "DS06": 333 } ]; Let's say, I have another array of objects.(比方说,我还有另一个对象数组。) var jsonData2 = [{ "Mass": 3, "Force": 3.1, "Acceleration": 4 }, { "Mass": 3, "Force": 4.1, "Acceleration": 4 }]; So after reducing it should be:(所以减少后应该是:) var jsonData2 = [{ "Mass": 3, "Force": [3.1, 4.1], "Acceleration": 4 }]; I have been trying to do these by using Array.reduce() but not getting an idea on how to do this job efficiently.(我一直在尝试通过使用Array.reduce()来做到这一点,但对如何有效地完成这项工作一无所知。) Is it possible to(是否有可能) making a single function(使一个功能) passing these kinds of array of objects as a parameter(将这类对象数组作为参数传递) and finally getting the reduced dataset(最后得到简化的数据集) What I have tried :(我尝试过的) var jsonData2 = [{ "Mass": 3, "Force": 3.1, "Acceleration": 4 }, { "Mass": 3, "Force": 4.1, "Acceleration": 4 }]; const reduced = jsonData2.reduce((r, e, i, a) => { if (i % 2 == 0) { const next = a[i + 1]; const obj = { ...e, Force: [e.Force] } if (next) obj.Force.push(next.Force); r.push(obj) } return r; }, []); console.log(reduced);   ask by Shunjid translate from so

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

1 Reply

0 votes
by (71.8m points)

You could get common keys and group by them.(您可以获取公用密钥并按它们进行分组。)

var data = [{ DS01: 123, DS02: 88888, DS03: 1, DS04: 2, DS05: 3, DS06: 666 }, { DS01: 123, DS02: 88888, DS03: 2, DS04: 3, DS05: 4, DS06: 666 }, { DS01: 124, DS02: 99999, DS03: 3, DS04: 4, DS05: 5, DS06: 333 }, { DS01: 124, DS02: 99999, DS03: 5, DS04: 6, DS05: 7, DS06: 333 }], common, temp = data.reduce((r, o, i) => { Object.entries(o).forEach(([k, v]) => { r[k] = r[k] || []; r[k][i] = v; }); return r; }, {}), min = Infinity, result; Object.entries(temp).forEach(([k, a]) => { var s = new Set; temp[k] = a.map(v => s.add(v).size); min = Math.min(min, s.size); }); common = Object.keys(temp).filter(k => temp[k][temp[k].length - 1] === min); result = data.reduce((r, o) => { var temp = r.find(q => common.every(k => q[k] === o[k])); if (!temp) { r.push({ ...o }); } else { Object.keys(o).filter(k => !common.includes(k)).forEach(k => Array.isArray(temp[k]) ? temp[k].push(o[k]) : (temp[k] = [temp[k], o[k]])); } return r; }, []); console.log(result); .as-console-wrapper { max-height: 100% !important; top: 0; }

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

...