简体   繁体   English

循环遍历多个对象并在新的 object 中获取相似的键值

[英]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.

相关问题 如何遍历多个对象并将键值与字符串进行比较 - How to loop through multiple Objects and compare key values to string 循环一个对象数组并根据比较对象值得到一个新的 object - Loop an array of objects and get a new object based on comparing objects values 如何遍历对象数组并在React中获取特定的键值? - How to loop through an array of objects and get specific key values in React? 如何遍历 JavaScript object 中的相似键值对(a0,a1,a2)并生成一个没有键中数字的新数组(a)? - How do I loop through similar key value pairs(a0,a1,a2) in JavaScript object and generate a new array without the number in the key(a)? 遍历键值为数组的对象(其值为对象) - Loop through object whose key's values are arrays (the values of which are objects) 循环对象数组并创建一个新的 object - Loop through an array of objects and create a new object 遍历每个元素并将值添加到新对象。 将这些对象添加到数组并访问其值 - Loop through each element and add values to a new object. Add these objects to array & access their values 如何遍历对象数组并根据 JavaScript 中的条件添加新的对象键? - How to loop through array of objects and add new object key based on condition in JavaScript? 以数组作为值循环遍历对象的对象 - Loop through an object of objects with arrays as values 对具有相似键值的对象求和 - Sum objects with similar key values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM