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

Combine values of two dynamic keys in an object

I have an object that looks like this:

const query = {
  item: {
    'available': false
  },
  status: {
    'locked': true
  }
}

I want to fetch the nested keys available and locked and create a new object with their values, resulting in

{
  'available': false,
  'locked': true
}

Whats the most precise way to do this? Bear in mind that the keys and the nested keys are dynamic, i.e. can change in future depending on another object.

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 could get the values and create a new object.

const
    query = { item: { available: false }, status: { locked: true } },
    joined = Object.assign({}, ...Object.values(query));

console.log(joined);
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