1

I would like to know how to Merge properties of objects with in same array of objects if same value in javascript.

My input data

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' },
]

Expected Output:

[
   { 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'},
]

I tried below code

    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))
1
  • You will lose some data (answers are different for each object) with current expected output. Object structure should be like name:'Alex', courses:[{subject:'Math',marks: '90', answer: 'Extreme' }, {subject:'Science',marks:'90',answer: 'Moderate' }]... Also student name can be same, there must be some identifier. Commented Dec 1, 2021 at 13:15

1 Answer 1

3

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)

Sign up to request clarification or add additional context in comments.

6 Comments

looks good, himanshu, need add one more field i.e. answera
what if the input data object is having more key value pair what should I do for example ` { student: 'Alex', subject: 'maths', marks: '85', answer: 'Moderate',loe:'xyz',comments:'well done' }, `
edited the post for this usecase
I updated the post with new dataset
This solution is a sample, you can understand it with the help of comments. It will be easy to update as per new data. You can try it. You can check the comments again, I have updated it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.