简体   繁体   English

基于两个或多个键的对象数组和动态收集的值数组对对象进行分组

[英]Group objects from an array of objects based on two or more keys and from an array of dynamically collected values

I need to group objects based on two or more filters on known keys from the values of the dataset.我需要根据数据集值中已知键上的两个或多个过滤器对对象进行分组。

I believe filtering is the best approach but all of my attempts have failed.我相信过滤是最好的方法,但我所有的尝试都失败了。

var uniqueKeys = ['course','gradDate'];

var dataSet = [
  {
    lastName:'Jones',
    course:'Standards',
    gradDate:'12/12/2022'
  },
{    
    lastName:'Smith',
    course:'Standards',
    gradDate:'12/12/2022'
  },
{
    lastName:'Martinez',
    course:'Maths',
    gradDate:'12/12/2022'
  },
{
    lastName:'Santiago',
    course:'Photography',
    gradDate:'12/11/2022'
  },
{
    lastName:'Alexi',
    course:'Photography',
    gradDate:'12/11/2022'
  }
]

function returnUniqueValues(array,key){
    return UniqueValue = [...new Set(array.map(item => item[key]))]
}

var uniqueCourses = returnUniqueValues(dataSet,'course') 
//result for this dataset === ["Standards","Maths","Photography"];
var uniqueGradDates = returnUniqueValues(dataSet,'gradDate') 
//Result for this dataset === ["12/12/2022","12/11/2022"];

From here I have two arrays of unique values that I need to group objects from the source dataset (dataSet)从这里我有两个唯一值数组,我需要对源数据集(dataSet)中的对象进行分组

I apologize if I am not describing this correctly, hopefully the example of the expected output will clear it up.如果我没有正确描述这一点,我深表歉意,希望预期输出的示例能够解决这个问题。

For all steps, if nothing matches the double filter, nothing should return.对于所有步骤,如果没有任何内容与双重过滤器匹配,则不应返回任何内容。 Example step through at least with how I have been approaching this problem, follows:至少通过我如何处理这个问题的示例步骤如下:

The first step would filter dataSet for all objects that have a course === "Standards" and a gradDate === "12/12/2022".第一步将为所有具有 course === "Standards" 和 gradDate === "12/12/2022" 的对象过滤数据集。 Here I would would get two objects back.在这里,我会取回两个对象。 I would then pass that to a new array for reference later.然后我会将其传递给一个新数组以供稍后参考。

Second step would filter the source dataset, dataSet for any object that has course === "Standards" and gradDate === "12/11/2022".第二步将过滤源数据集,dataSet 用于具有 course === "Standards" 和 gradDate === "12/11/2022" 的任何对象。

Third step would filter dataSet for all objects where the course === "Maths" and gradDate === "12/12/2022"第三步将为 course === "Maths" 和 gradDate === "12/12/2022" 的所有对象过滤数据集

Example output for this dataset would by此数据集的示例输出将由

var finalData = [
    [{
            lastName: 'Jones',
            course: 'Standards',
            gradDate: '12/12/2022'
        },
        {
            lastName: 'Smith',
            course: 'Standards',
            gradDate: '12/12/2022'
        },
    ],
    [{
        lastName: 'Martinez',
        course: 'Maths',
        gradDate: '12/12/2022'
    }],
    [{
            lastName: 'Santiago',
            course: 'Photography',
            gradDate: '12/11/2022'
        },
        {
            lastName: 'Alexi',
            course: 'Photography',
            gradDate: '12/11/2022'
        }
    ]
]

End goal is to then loop through each group (in this case of anybody with the same course and gradDate pairs) and populate a report with their information like lastname.最终目标是然后遍历每个组(在这种情况下,任何具有相同课程和 gradDate 对的人)并使用他们的信息(如姓氏)填充报告。 In this example there are 3 unique combinations of course and gradDate that actually exist.在此示例中,实际存在 3 个唯一的 course 和 gradDate 组合。 Scaling, I full expect thousands of different courses with up to 4 different dates possible.缩放,我完全期望有多达 4 个不同日期的数千种不同的课程。

You can simply use a 'group-by' with a compound key of the specified properties.您可以简单地使用带有指定属性的复合键的“分组依据”。

Here calling map() and join() on your uniqueKeys array for each iterated object to generate the compound key, then retrieving or initializing the property using logical nullish assignment (??=) before pushing the object to it.这里为每个迭代对象调用uniqueKeys数组上的map()join()以生成复合键,然后在将对象推送给它之前使用逻辑空赋值 (??=)检索或初始化属性。 The result is the Object.values() of the grouped object.结果是分组对象的Object.values()

 const dataSet = [{ lastName: 'Jones', course: 'Standards', gradDate: '12/12/2022' }, { lastName: 'Smith', course: 'Standards', gradDate: '12/12/2022' }, { lastName: 'Martinez', course: 'Maths', gradDate: '12/12/2022' }, { lastName: 'Santiago', course: 'Photography', gradDate: '12/11/2022' }, { lastName: 'Alexi', course: 'Photography', gradDate: '12/11/2022' }]; const uniqueKeys = ['course', 'gradDate']; const grouped = {}; for (const o of dataSet) { const key = uniqueKeys.map(k => o[k]).join('_'); (grouped[key] ??= []).push(o); } const result = Object.values(grouped) console.log(result);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM