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

Node JS – Parse object where keys are arrays of objects using those objects properties as conditions

I have an Object where each key is an Actor name and its property is an array of objects with some infos, like below:

const x = {
  'Jonathan Majors': [
    {
      character: 'Kang the Conqueror',
      title: 'Ant-Man and the Wasp: Quantumania',
      release_date: '2023-07-26'
    },
    {
      character: 'He Who Remains (archive footage)',
      title: "Marvel Studios' 2021 Disney+ Day Special",
      release_date: '2021-11-12'
    }
  ],
  'Vin Diesel': [
    {
      character: 'Groot (voice)',
      title: 'Guardians of the Galaxy Vol. 3',
      release_date: '2023-05-03'
    },
    {
      character: 'Self',
      title: 'Marvel Studios: Assembling a Universe',
      release_date: '2014-03-18'
    }
  ]
}

The condition to the new object is to return only the Actors that have more than one character, excluding the character Self .. so if the Actor has two chars, but one is self, it should be deleted

const result = {
  'Jonathan Majors': [
    {
      character: 'Kang the Conqueror',
      title: 'Ant-Man and the Wasp: Quantumania',
      release_date: '2023-07-26'
    },
    {
      character: 'He Who Remains (archive footage)',
      title: "Marvel Studios' 2021 Disney+ Day Special",
      release_date: '2021-11-12'
    }
  ]
}

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 :

You can use a combination of Object.entries, Object.fromEntries, filter functions and a set.

const result = Object.fromEntries(
    Object.entries(x)
    .filter(([,data]) =>
       new Set(data.filter(({character}) => character !== 'Self')).size > 1
    )
)
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