Home > OS >  Merge array of javascript objects
Merge array of javascript objects

Time:04-29

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

CodePudding user response:

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);

CodePudding user response:

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);

  • Related