Javascript equivalent to Ruby `send`

Trying to loop through the entire updated field that I have and update them dynamically before saving.

Product.findOne({ _id: productNewData['_id'] }, function (err, doc) { for (var key in productNewData) { # what do I do here? } doc.save(); }); 

I know that Ruby has a submit method like this:

 doc.send(key) = productNewData[key] 

I think I can check the parameters and use eval . Is there another way?

+6
source share
1 answer

They have two ways to access properties in Javascript: Using dot notation or parentheses. Example:

 var foo = {bar: 42} foo.bar // 42 foo["bar"] // 42 var v = "bar" foo[v] // 42 foo.v // undefined 

So:

 Product.findOne({ _id: productNewData['_id'] }, function (err, doc) { for (var key in productNewData) { doc[key] = productNewData[key] } doc.save(); }); 
+10
source

All Articles