1

This is the array of javascript objects. I want these javascript objects will merge into a single javascript object according to their same property value.

This is the original array:

const array = [
 {email: '[email protected]', name: 'Jon', role: 'Player', Education: {…}},
 {email: '[email protected]', name: 'Jon', role: 'Player', Skills: {…}},
 {email: '[email protected]', name: 'Jon', role: 'Player', Total: {…}},
 {email: '[email protected]', name: 'Jon', role: 'Player', Personal: {…}},
 {email: '[email protected]', name: 'Tayyab', role: 'Lead', Ethics: {…}},
 {email: '[email protected]', name: 'Tayyab', role: 'Lead', Total: {…}},
 {email: '[email protected]', name: 'Arya', role: 'Banker', Total: {…}},
 {email: '[email protected]', name: 'Arya', role: 'Banker', Skills: {…}},
 {email: '[email protected]', name: 'Arya', role: 'Banker', Personal: {…}},
 {email: '[email protected]', name: 'Arya', role: 'Banker', Education: {…}},
]

This is the required array:

const array = [
 {email: '[email protected]', name: 'Jon', role: 'Player', Education: {…}, Skills: {…}, Total: {…}, Personal: {…}},
 {email: '[email protected]', name: 'Tayyab', role: 'Lead', Ethics: {…}, Total: {…}},
 {email: '[email protected]', name: 'Arya', role: 'Banker', Total: {…}, Skills: {…}, Personal: {…}, Education: {…}},
]

Like email, name, and role all three have the same property and property value. It would merge into one javascript object and the others remain the same. Kindly provide the solution in React.js/Javascript. Thanks

2 Answers 2

4

A good case to apply reduce method, try this code:

const array = [
 {email: '[email protected]', name: 'Jon', role: 'Player', Education: {}},
 {email: '[email protected]', name: 'Jon', role: 'Player', Skills: {}},
 {email: '[email protected]', name: 'Jon', role: 'Player', Total: {}},
 {email: '[email protected]', name: 'Jon', role: 'Player', Personal: {}},
 {email: '[email protected]', name: 'Tayyab', role: 'Lead', Ethics: {}},
 {email: '[email protected]', name: 'Tayyab', role: 'Lead', Total: {}},
 {email: '[email protected]', name: 'Arya', role: 'Banker', Total: {}},
 {email: '[email protected]', name: 'Arya', role: 'Banker', Skills: {}},
 {email: '[email protected]', name: 'Arya', role: 'Banker', Personal: {}},
 {email: '[email protected]', name: 'Arya', role: 'Banker', Education: {}},
]

let result = Object.values(array.reduce((acc, {name, ...rest}) => {
    acc[name] = {...acc[name], ...{name, ...rest}};
    return acc;
}, {}));

console.log(result);

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

3 Comments

The end results would be an object instead of an array, but this could easily be updated to work as the question wants
@Joe Lissner result is an Array
Yup, my bad! Missed the Object.values
0

Using reduce and Object.values

const array = [
 {email: '[email protected]', name: 'Jon', role: 'Player', Education: {}},
 {email: '[email protected]', name: 'Jon', role: 'Player', Skills: {}},
 {email: '[email protected]', name: 'Jon', role: 'Player', Total: {}},
 {email: '[email protected]', name: 'Jon', role: 'Player', Personal: {}},
 {email: '[email protected]', name: 'Tayyab', role: 'Lead', Ethics: {}},
 {email: '[email protected]', name: 'Tayyab', role: 'Lead', Total: {}},
 {email: '[email protected]', name: 'Arya', role: 'Banker', Total: {}},
 {email: '[email protected]', name: 'Arya', role: 'Banker', Skills: {}},
 {email: '[email protected]', name: 'Arya', role: 'Banker', Personal: {}},
 {email: '[email protected]', name: 'Arya', role: 'Banker', Education: {}},
]

const result = Object.values(array.reduce((acc, item) => {
    // build key with unique props
    const key = [item.email, item.name, item.role].join("-");
    // assign properties to a new object
    // if object does not exist with the key it uses empty object
    acc[key] = {...(acc[key] || {}), ...item}; 
    return acc;
}, {}));

console.log(result);

2 Comments

Probably the same person that downvted the question and the other answer.
yeah it seems good to me. Have another upvote! The comments made it clearer for me to understand.

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.