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

javascript - 通过使用对象源数组中的属性以特定顺序进行分组来形成数组(Forming an array by grouping using the properties from source array of objects in a particular order)

I have an array of objects with the following structure that is being sent as response:(我有一个具有以下结构的对象数组,这些对象将作为响应发送:)

let sampleData = [ { values: { High: 4, Low: 5, Medium: 7 } , time: "1571372233234" , sum: 16 }, { values: { High: 5, Low: 3, Medium : 1 }, time: "1571372233234" , sum: 9}, { time: "14354545454", sum: 0}, { time: "14354545454", sum: 0} } ]; I need to take each key within each object inside array and form an array out of it.(我需要将每个键放在数组中的每个对象内,并从中形成一个数组。) Basically grouping based on the key present in all the objects.If the object has no "values" it should return 0 across the val1,val2,val3.(基本上基于所有对象中存在的键进行分组。如果对象没有“值”,则应在val1,val2,val3中返回0。) But the issue , the order of the keys "high,low,medium" inside "values" object in the question is unpredictable.(但是问题中,问题中“值”对象中键“高,低,中”的顺序是无法预测的。) With whatever code I have written keys that are present in the first object will be grouped and follows that order.(无论我写了什么代码,第一个对象中存在的键都将被分组并遵循该顺序。) But I want be it any input order, It should always give me this order "High Medium Low" Order.(但我希望它是任何输入顺序,它应该始终给我该顺序“高中低”顺序。) The resultant array of objects should look like and should always follow "High Medium Order":(对象的结果数组应类似于并且应始终遵循“高中阶”:) result = [ { name: 'High', data: [4, 5, 0, 0] }, { name: 'Medium', data: [5, 3, 0, 0] }, { name: 'Low', data: [7, 1, 0, 0] } ] I have tried the following:(我尝试了以下方法:) let sampleData = [{ values: { High: 4, Low: 5, Medium: 7 } , time: "1571372233234" , sum: 16 },{ values: { High: 5, Low: 3, Medium : 1 }, time: "1571372233234" , sum: 9},{ time: "14354545454", sum: 0},{ time: "14354545454", sum: 0 }]; const keySet = new Set(sampleData.flatMap(a => Object.keys(a.values || {}))); const merged = sampleData.reduce((acc, { values = {} }) => { keySet.forEach(name => { acc[name] = acc[name] || { name, data: [] }; acc[name].data.push(values[name] || 0); }); return acc; }, {}); console.log(merged);   ask by v2103 translate from so

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

1 Reply

0 votes
by (71.8m points)

You could take the result set and iterate for the key and array for the value.(您可以获取结果集,然后迭代键和值数组。)

let sampleData = [{ values: { High: 4, Low: 5, Medium: 7 }, time: "1571372233234", sum: 16 }, { values: { High: 5, Low: 3, Medium : 1 }, time: "1571372233234", sum: 9 }, { time: "14354545454", sum: 0 }, { time: "14354545454", sum: 0 }], keys = ['High', 'Low', 'Medium'], grouped = sampleData.reduce((r, { values = {} } = {}) => { r.forEach(({ name, data }) => data.push(values[name] || 0)); return r; }, keys.map(name => ({ name, data: [] }))); console.log(grouped); .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

...