Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to merge elements in objects inside of array and returned unique objects with updated data

my question is how I can merge elements in array of objects and return array with unique objects. I was trying to use reduce but it’s looks like I missing something and it’s time to ask for hint.
So how you can see I need merge elements of boolean where false is default value and true is new data, and return unique objects based on name. Thank you for help.

const arr = [
    {
      code: 'test1',
      name: 'TEST1',
      url: '/test1',
      elements: {
        option1: true,
        option2: false,
        option3: false,
        option4: false,
        option5: false
      }
    },
      {
        code: 'test1',
        name: 'TEST1',
        url: '/test1',
        elements: {
          option1: false,
          option2: false,
          option3: true,
          option4: false,
          option5: false
      }
      },
        {
        code: 'test1',
        name: 'TEST1',
        url: '/test1',
        elements: {
          option1: false,
          option2: false,
          option3: false,
          option4: true,
          option5: false
      }
      },
       {
        code: 'test2',
        name: 'TEST2',
        url: '/test2',
        elements: {
          option1: false,
          option2: false,
          option3: true,
          option4: false,
          option5: false
      }
      },
    ]

and output it should be:

const arr = [
    {
      code: 'test1',
      name: 'TEST1',
      url: '/test1',
      elements: {
        option1: true,
        option2: false,
        option3: true,
        option4: true,
        option5: false
      }
    },
       {
        code: 'test2',
        name: 'TEST2',
        url: '/test2',
        elements: {
          option1: false,
          option2: false,
          option3: true,
          option4: false,
          option5: false
      }
      },
    ]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Working on the assumption that elements is the only thing that actually varies here, and that you can’t switch back from false to true (since that what it seems like you are going for), you’d need to iterate over the key-value pairs in elements and set it to the originalValue || newValue, so something like this:

const arr = [
    {
      code: 'test1',
      name: 'TEST1',
      url: '/test1',
      elements: {
        option1: true,
        option2: false,
        option3: false,
        option4: false,
        option5: false
      }
    },
      {
        code: 'test1',
        name: 'TEST1',
        url: '/test1',
        elements: {
          option1: false,
          option2: false,
          option3: true,
          option4: false,
          option5: false
      }
      },
        {
        code: 'test1',
        name: 'TEST1',
        url: '/test1',
        elements: {
          option1: false,
          option2: false,
          option3: false,
          option4: true,
          option5: false
      }
      },
       {
        code: 'test2',
        name: 'TEST2',
        url: '/test2',
        elements: {
          option1: false,
          option2: false,
          option3: true,
          option4: false,
          option5: false
      }
      },
    ].reduce((carry, current) => {
        const existing = carry.find(item => item.code === current.code);
        if (!existing) {
            carry.push(current);
        } else {
            Object.entries(current.elements).forEach(([k, v]) => {
                existing.elements[k] = existing.elements[k] || current.elements[k];
            });
        }
        return carry;
    }, []);
    
    console.log(arr);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading