[英]Loop through multiple objects and get similar key values in new object
Hi I have array of objects and I am trying to loop through them and have similar key values in new object here is example of data that I have.嗨,我有对象数组,我正在尝试遍历它们并在新的 object 中具有相似的键值,这是我拥有的数据示例。
let newDats = [{"ID":1, "Name": "Ahmed", "Age":17, "Score":84, "Absentee":3},
{"ID":2, "Name": "Hassan", "Age":15, "Score":87, "Absentee":2},
{"ID":3, "Name": "Aisha", "Age":18, "Score":86, "Absentee":2}]
And so on.等等。 However, what I want is something as:但是,我想要的是:
data = [{ID:[1,2,3], Names:["Ahmed", "Hassan","Aisha"],
Ages:[17,15,18]}]
And so forth.等等。 My end goal is to perform basic descriptive statistics on the numeric parts of the data so if there is better ideas I would love to hear I am knida newbie to js.我的最终目标是对数据的数字部分执行基本的描述性统计,所以如果有更好的想法,我很想听听我是 js 的新手。
PS附言
Column names (object keys ) can be more than this and unknown so I want something dynamic.列名(对象键)可能不止于此而且未知,所以我想要一些动态的东西。
Thanks in advance.提前致谢。
Something like:就像是:
let newDats = [{"ID":1, "Name": "Ahmed", "Age":17, "Score":84, "Absentee":3}, {"ID":2, "Name": "Hassan", "Age":15, "Score":87, "Absentee":2}, {"ID":3, "Name": "Aisha", "Age":18, "Score":86, "Absentee":2}]; data = {}; keys = []; for(let keyVal in newDats[0]) { keys.push(keyVal) data[keyVal] = []; } newDats.forEach(dt => { keys.forEach( kv => { data[kv].push(dt[kv]) }) }) console.log(data)
You can simply reduce()
the array of objects, iterating over all Object.keys()
of each element and pushing them to the property array of the accumulator.您可以简单地reduce()
对象数组,遍历每个元素的所有Object.keys()
并将它们推送到累加器的属性数组。 This allows for each object having different keys.这允许每个 object 具有不同的密钥。
let newDats = [{ "ID": 1, "Name": "Ahmed", "Age": 17, "Score": 84, "Absentee": 3 }, { "ID": 2, "Name": "Hassan", "Age": 15, "Score": 87, "Absentee": 2 }, { "ID": 3, "Name": "Aisha", "Age": 18, "Score": 86, "Absentee": 2 }]; const result = newDats.reduce((acc, o) => ( Object.keys(o).forEach(k => (acc[k]??= []).push(o[k])), acc), {}); console.log(result);
.as-console-wrapper { max-height: 100%;important: top; 0; }
Note the use of the logical nullish assignment operator (??=) which can be replaced by a logical OR short circuit for compatibility.注意逻辑空赋值运算符 (??=)的使用,为了兼容性,它可以用逻辑 OR 短路代替。
//acc[k] ??= []
acc[k] = acc[k] || []
You can use the function below.您可以使用下面的 function。 Just pass the array
you want to sort.只需传递您要排序的array
。
function dataSorter(dataArr) { const data = {}; for (const item in dataArr[0]) data[item] = []; for (let i = 0; i < dataArr.length; i++) { for (const item in dataArr[i]) { data[item].push(dataArr[i][item]); } } return data; } /* Example Below */ let newDats = [{ ID: 1, Name: "Ahmed", Age: 17, Score: 84, Absentee: 3 }, { ID: 2, Name: "Hassan", Age: 15, Score: 87, Absentee: 2 }, { ID: 3, Name: "Aisha", Age: 18, Score: 86, Absentee: 2 }, ]; console.log(dataSorter(newDats));
.as-console-wrapper { max-height: 100%;important: top; 0; } // Ignore this
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.