Home > Software engineering >  get object values by other object key
get object values by other object key

Time:12-22

I have two objects

object1 value

 {
"type_Id": 10,
"type_Name": "Admission Fee-One Time (1)",
"old Admission": "500.00",
"new Admission": "1000.00",
"staff Fee": "200.00", 
"free Education": "50.00"
 }

object2 value

{ 
"fee_category_Id": 2,
"fee_category_Name": "Old Admission", 
"fee_Descripition": "Admission FEE"
}

object1 key names are dynamic and could vary.

I want to get the value from object1 by object2 key name

I tried to do this but it is not working my attempt

   Object.keys(this.object1).forEach((key) => {
   Object.keys(this.object2).forEach((keys) => { 
  if(this.object1[key][this.object2[keys].fee_category_Name]){///but it never true

 //get the fee amount
 }
 });
});

What I am missing in this.

CodePudding user response:

You can do the following, get keys from object2 and check for their existence in object1.

Object.keys(object2).forEach((key) => {
  if(object1.hasOwnProperty(object2[key])) {
    const value = object1[object2[key]];
    // do whatever you like with 'value'
  }
});
  • Related