How do I create new object after manipulation

I have a JavaScript object like the following below availability and reserved, here I need to subtract quantity value from availability.

var availability = {"bike":10,"cycle":3,"car":1};

var reserved ={"cycle":1,"bike":10}

how should I get this response as below?

response = {"bike":0,"cycle":2,"car":1};

Answer

You can iterate through each key-value pair and subtract quantity in availability with the corresponding key in reserved. Then create your result object using Object.fromEntries().

const availability = { "bike" : 10, "cycle" : 3, "car" : 1 },
      reserved ={ "cycle": 1,"bike": 10 },
      result = Object.fromEntries(Object.entries(availability).map(([key, value]) => [key, value - (reserved[key] ?? 0)]));
console.log(result);