Home > Software engineering >  Combine values of two dynamic keys in an object
Combine values of two dynamic keys in an object

Time:12-08

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.

CodePudding user response:

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

  • Related