Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript equivalent to Ruby's `send` [duplicate]

Tags:

javascript

Trying to loop on all updated field I've got 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 send method like this:

doc.send(key) = productNewData[key]

I guess I can validate the params given and use eval. Is there any other way?

like image 992
WebQube Avatar asked May 03 '14 20:05

WebQube


People also ask

What is the difference between Ruby and JavaScript?

JavaScript and Ruby are object-oriented, dynamic and general-purpose scripting language, which is interpreted rather than compile during runtime. JavaScript can be used as front-end and back-end language using the same language, whereas Ruby is used as a back-end programming language.

Should I use JavaScript or ruby for full stack development?

Ruby applications are difficult to debug as it has multiple layers of abstraction due to which it will take more time to fix errors, whereas JavaScript applications are easy to debug compared to Ruby. JavaScript can be used for Full stack development due to its node.JS framework, whereas Ruby can’t be used as a Full Stack.

What are some companies that use JavaScript instead of Ruby?

Many companies use JavaScript; some of them are Instagram, eBay, Codecademy, Firebase, Groove shark, Square, sentry etc. Whereas many companies use Ruby, some of them are Intuit, Rap Genius, Instacart, Task Rabbit, Fab, Scribd etc.

What is the difference between Ruby and Node JS?

Ruby is better for high CPU intensive application development, which involves graphics, image processing etc., whereas Node.JS is not suitable for high CPU application development. JavaScript can be integrated with many applications such as Auth0, Parcel, Yarn, Buttercup, cell and Apache Open whisk etc.


1 Answers

They are two way to acces properties in Javascript: Using dot notation or brackets. 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();
});
like image 76
Vinz243 Avatar answered Nov 10 '22 10:11

Vinz243