If I know there's a property in common between two JavaScript objects called "req" and "updatedDoc" respectively, is there a way I can use a placeholder parameter to represent any key, so that I can find the right one that matches on the two objects? I tried this but it doesn't work:
for (const [key, val] of Object.entries(req)) {
if (key === updatedDoc[key]) {
console.log("key, val", key, val);
}
}
By the way, in my use case I know there will always be one matching property between the two objects. And to clarify, the two objects are called "req" and "updatedDoc". I don't know what they key will be, but I know the two objects will have one key in common.
To add a little more clarity, "req" is going to be something simple, like:
const req = {
"deleted" : true,
"apiKey" : "4d9d9291",
"token" : "ffdsfjsdfsdjfa"
}
... whereas updatedDoc
will be a full document, like this:
const updatedDoc = {
_id: <ObjectId>,
firstName: "John",
lastName: "Smith",
age: 42
deleted: false
}
Both have a property called "deleted". Basically I'm matching a request passed in with the whole document it pertains to. I then want to take in the value from "req" and save it to "updatedDoc" for the correct key. But first I need to find the matching key, and pull out the value from "req". Is there a way I can do this?