使用值将数组中对象的属性合并在一起并删除重复项

Posted

技术标签:

【中文标题】使用值将数组中对象的属性合并在一起并删除重复项【英文标题】:Merge properties of objects within a array together using values and remove duplicate 【发布时间】:2022-01-08 01:25:44 【问题描述】:

如果javascript中的值相同,我想知道如何将对象的属性与相同的对象数组合并。

我的输入数据

var input = [
       student: 'Alex', class: 'ten', subject: 'maths', marks: '85', answer: 'Moderate', percentage: '90', sports: 'good' ,
     student: 'Alex', class: 'ten', subject: 'maths', marks: '85', answer: 'Moderate', percentage: '90', sports: 'good' ,
     student: 'Alex', class: 'nine', subject: 'science', marks: '82', answer: 'Severe', percentage: '85', sports: 'good' ,
     student: 'Alex', class: 'nine', subject: 'science', marks: '82', answer: 'Severe', percentage: '85', sports: 'good' ,
     student: 'Alex', class: 'eight', subject: 'computer', marks: '90', answer: 'Extreme', percentage: '87', sports: 'good' ,
     student: 'Alex', class: 'eight', subject: 'computer', marks: '90', answer: 'Extreme', percentage: '87', sports: 'good' ,
     student: 'john', class: 'ten', subject: 'maths', marks: '90', answer: 'Extreme', percentage: '99', sports: 'good' ,
     student: 'john', class: 'ten', subject: 'maths', marks: '90', answer: 'Extreme', percentage: '99', sports: 'good' ,
     student: 'john', class: 'nine', subject: 'science', marks: '85', answer: 'Moderate', percentage: '100', sports: 'good' ,
     student: 'john', class: 'nine', subject: 'science', marks: '85', answer: 'Moderate', percentage: '100', sports: 'good' ,
     student: 'john', class: 'eight', subject: 'computer', marks: '87', answer: 'Extreme', percentage: '67', sports: 'good' ,
     student: 'john', class: 'eight', subject: 'computer', marks: '87', answer: 'Extreme', percentage: '67', sports: 'good' ,
]

预期输出:

[
    student: 'Alex',class: 'ten', maths: '85', science: '82',computer:'90', answer: 'Moderate', percentage: '90', sports: 'good',
     student: 'Alex',class: 'nine', maths: '85', science: '82',computer:'90', answer: 'Moderate', percentage: '85', sports: 'good',
     student: 'Alex',class: 'eight', maths: '85', science: '82',computer:'90', answer: 'Moderate', percentage: '87', sports: 'good',
     student: 'john',class: 'ten',maths: '90', science: '85',computer:'87',answer: 'Extreme', percentage: '99', sports: 'good',
     student: 'john',class: 'nine',maths: '90', science: '85',computer:'87',answer: 'Extreme', percentage: '100', sports: 'good',
     student: 'john',class: 'eight',maths: '90', science: '85',computer:'87',answer: 'Extreme', percentage: '67', sports: 'good',
]

我试过下面的代码

    const output = data.reduce((a, v) => 
    const key = i.student + i.subject;
    const subjectKey = v.subject
    if (a[key]) 
        a[key] = 
            ...v,
            [subjectKey]: v.marks,
        
     else 
        a[key] = v
    
    return a
, )
console.log(Object.values(output))

【问题讨论】:

您将丢失一些当前预期输出的数据(answers 对于每个对象都不同)。对象结构应该是name:'Alex', courses:[subject:'Math',marks: '90', answer: 'Extreme' , subject:'Science',marks:'90',answer: 'Moderate' ]... 学生名也可以相同,必须有一些标识符。 【参考方案1】:

var input=[student:"Alex",subject:"maths",marks:"85",answer:"Moderate",student:"Alex",subject:"maths",marks:"85",answer:"Moderate",student:"Alex",subject:"maths",marks:"85",answer:"Moderate",student:"Alex",subject:"science",marks:"82",answer:"Severe",student:"Alex",subject:"science",marks:"82",answer:"Severe",student:"Alex",subject:"science",marks:"82",answer:"Severe",student:"Alex",subject:"computer",marks:"90",answer:"Extreme",student:"Alex",subject:"computer",marks:"90",answer:"Extreme",student:"Alex",subject:"computer",marks:"90",answer:"Extreme",student:"john",subject:"maths",marks:"90",answer:"Extreme",student:"john",subject:"maths",marks:"90",answer:"Extreme",student:"john",subject:"maths",marks:"90",answer:"Extreme",student:"john",subject:"science",marks:"85",answer:"Moderate",student:"john",subject:"science",marks:"85",answer:"Moderate",student:"john",subject:"science",marks:"85",answer:"Moderate",student:"john",subject:"computer",marks:"87",answer:"Extreme",student:"john",subject:"computer",marks:"87",answer:"Extreme",student:"john",subject:"computer",marks:"87",answer:"Extreme"];

//find all unique inputs, i.e. entry with unique strudent and subject
var uniqueInput = [...input].reduce((unique, obj) => 
  if (unique.filter((student,subject) => student === obj.student && subject === obj.subject /* && otherKeyFeatureUsedForUniqueness */).length === 0) return [...unique, obj]; //if not already inserted
    return unique; //already inserted
, [])

var finalArray = [...uniqueInput].reduce((alreadyInsertedStudent, currentStudent) => 
    //get array of students without current student
    var arrWithoutObj = alreadyInsertedStudent.filter((student) => student !== currentStudent.student /* && the student or class or other properties which is constant across the duplicated rows in original input array, you can add here*/); 
    
    //if the student is not present, then withoutArray === currentarray
    //if yes, then insert the student and subject 
  if (arrWithoutObj.length === alreadyInsertedStudent.length) return [...alreadyInsertedStudent, student: currentStudent.student, [currentStudent.subject]: currentStudent.marks, answer: currentStudent.answer/*, ...rest additional unique row defining Properties of final answer, you can add here*/];
    //if student already inserted, then add the subject marks to that student 
    return [...alreadyInsertedStudent].map((prev) => prev.student === currentStudent.student ? ...prev, [currentStudent.subject]:currentStudent.marks : prev);
, [])

console.log(finalArray)

【讨论】:

看起来不错,himanshu,需要多加一个字段即answera 如果输入数据对象有更多的键值对我应该怎么做例如`学生:'Alex',主题:'数学',标记:'85',答案:'中等',loe:'xyz',cmets:'干得好', ` 为此用例编辑了帖子 我用新的数据集更新了帖子 这个解决方案是一个示例,您可以借助 cmets 理解它。根据新数据进行更新将很容易。你可以试试看。你可以再查看一下cmets,我已经更新了。

以上是关于使用值将数组中对象的属性合并在一起并删除重复项的主要内容,如果未能解决你的问题,请参考以下文章

从数组中删除重复的对象

删除对象数组重复项并将非重复值存储在数组中

如何合并子数组值并删除重复项?

如何删除重复结果并合并在 mySQL 查询中具有相似属性的对象?

合并两个 Eloquent Collections 并删除所有重复项。

删除javascript数组中重复对象的所有实例[重复]