简体   繁体   English

如何在一个键下对 JSON 数组中的所有对象的键下存在的值进行分组?

[英]How do I group values present under a key in all objects in an array of JSON, under a single key?

I have an array of JSON like:我有一个 JSON 数组,例如:

[
   {user_id:1, name: "Person A", project: "Project A"},
   {user_id:1, name: "Person A", project: "Project B"},
   {user_id:2, name: "Person B", project: "Project A"},
   {user_id:2, name: "Person B", project: "Project C"},
   {user_id:3, name: "Person C", project: "Project D"}
]

I want to transform this array such that there's only one entry for a person and it has an array with value active_projects or something with all his projects.我想转换这个数组,使得一个人只有一个条目,并且它有一个值为active_projects的数组,或者他所有的项目。 Basically I want to remove the redundancy in this array.基本上我想删除这个数组中的冗余。 The output would be: output 将是:

[
   {user_id:1, name: "Person A", active_projects: ["Project A","Project B"] },
   {user_id:2, name: "Person B", active_projects: ["Project A","Project C"] },
   {user_id:3, name: "Person C", active_projects: ["Project D"] }
]

What is the cleanest and best approach for this?什么是最干净和最好的方法?

A simple solution would be to first compute a map of the user_ids using reduce .一个简单的解决方案是首先使用reduce计算 user_ids 的 map。 Then simply use Object.values to turn it into an array.然后只需使用Object.values将其转换为数组。

 const lkp = data.reduce((acc, {user_id, name, project}) => { acc[user_id] = acc[user_id] || {}; acc[user_id].user_id = user_id; acc[user_id][name] = name; acc[user_id].active_projects = acc[user_id].active_projects || []; acc[user_id].active_projects.push(project); return acc; }, {}) const arr = Object.values(lkp) console.log(arr)
 <script> var data = [ {user_id:1, name: "Person A", project: "Project A"}, {user_id:1, name: "Person A", project: "Project B"}, {user_id:2, name: "Person B", project: "Project A"}, {user_id:2, name: "Person B", project: "Project C"}, {user_id:3, name: "Person C", project: "Project D"} ]; </script>

You can try reduce method of JavaScript.您可以尝试reduce JavaScript 的方法。

 var data =[ {user_id:1, name: "Person A", project: "Project A"}, {user_id:1, name: "Person A", project: "Project B"}, {user_id:2, name: "Person B", project: "Project A"}, {user_id:2, name: "Person B", project: "Project C"}, {user_id:3, name: "Person C", project: "Project D"}]; result = Object.values(data.reduce((acc, {user_id, project, ...rest})=>{ acc[user_id] = acc[user_id] || { user_id, ...rest }; acc[user_id].activeProject = acc[user_id].activeProject || []; acc[user_id].activeProject.push(project); return acc; },{})); console.log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将单个对象数组与其键值进行比较 - How do I compare single array of objects with it key values 如何按键对 JSON 数组进行分组? - How do I group a JSON array by a key? 如何获取与JSON中的键相关的数组中存在的所有值 - How to fetch all the values present in the array related to the key in json 从对象中获取所有唯一值并将它们分配到正确键下的数组中 - Get all unique values from objects and assign them into array under correct key 如何从具有多个键的 json 对象收集数据并将所有值推送到单个键:值数组 - how to collect data from json objects with multiple keys and push all values to single key:value array JSON返回多列,但所有值均作为单个字符串。 如何从数组中获取匹配的键值? - JSON returning multiple columns, but all values as single string. How do I get matching key-value from array? 如何在单键值对数组中获取嵌套的 json 对象值 - How can i get nested json objects values in single key value pair array 如何替换 JSON 对象数组中某个键的所有值 - How to substitute all values of a certain key in an array of JSON objects 如何在对象数组中组合/分组相同键的字符串值 - How do I combine/group string values of the same key within an array of objects 如果hashMap具有相同的键名,如何获取对象数组中的所有值 - How do I get all values in array of objects if hashMap has same key name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM