2

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?

13
  • Is updatedDoc is an object of objects ? Commented Feb 22, 2019 at 20:49
  • Are you only testing for a property AND value that matches? or just the keys. If just the keys, any of these would work: stackoverflow.com/questions/12433604/… to get you the matches, which you can then use to get the values etc. Commented Feb 22, 2019 at 20:50
  • Added more clarity above. Commented Feb 22, 2019 at 20:52
  • yeah, so just grab the keys of each and do a diff. If you know the one object will only have one key, it becomes even simpler; no need to loop. Commented Feb 22, 2019 at 20:53
  • key === updatedDoc[key] I think the problem here is that by this way you compare the key from req with the value from updatedDoc , while you want to compare the key of these two objects Commented Feb 22, 2019 at 20:53

3 Answers 3

2

You should be able to just modify your loop to change

if (key === updatedDoc[key]) to if (key in updatedDoc)

Everything inside this if statement will only be performed on keys that exist both in req and updatedDoc. The value stored for the key in req will be val, which was dereferenced from Object.entries

You can change updatedData to the new value like so updatedData[key] = val. You can also store the change in an array for later, if you like.

const updatedDoc = {
  firstName: "John",
  lastName: "Smith",
  age: 42,
  deleted: false
}
const req = {
  "deleted": true,
  "apiKey": "4d9d9291",
  "token": "ffdsfjsdfsdjfa"
}

const changes = []

for (const [key, val] of Object.entries(req)) {
  if (key in updatedDoc) {
    // get the previous value
    const oldVal = updatedDoc[key]
    // update updatedDoc
    updatedDoc[key] = val
    // store the change or do whatever
    changes.push({
      [key]: {
        new: val,
        old: oldVal
      }
    })
  }
}

console.log(updatedDoc)

console.log(changes)

Sign up to request clarification or add additional context in comments.

1 Comment

I like this, too. Nice one.
1

Why not take a Set for the first object and filter the keys for the second object.

The result is an array with common keys.

var objectA = { a: 1, b: 2, c: 3 },
    objectB = { b: 4, c: 5, d: 6 },
    common = Object.keys(objectB).filter(Set.prototype.has, new Set(Object.keys(objectA))),
    values = common.map(k => objectA[k]);

console.log(common);
console.log(values);

A bit shorter version.

var objectA = { a: 1, b: 2, c: 3 },
    objectB = { b: 4, c: 5, d: 6 },
    common = Object.keys(objectA).filter({}.hasOwnProperty.bind(objectB)),
    values = common.map(k => objectA[k]);

console.log(common);
console.log(values);

3 Comments

So this gives me the keys in common, which is good, but I'd also need to know the value for that matching key found in req. So, in your case here: [ { "b" : 4 }, { "c" : 5} ], assuming objectB represents req.
you could map the values, please see edit. do you want an object with common key and values form objectA?
Nice JavaScript Jiu Jitsu here. I think this will work, thanks.
0

Try this solution I think it will solve the problem

updatedDoc.forEach(function(i,v){
for (const [key, val] of Object.entries(req)) {
      if (key === i && req[key]==updatedDoc[i]) {
            console.log("key, val", key, val);
      }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.