title: Dynamically store new object property values in for...in loop in JavaScript
body: <p>I have a Node application running express and I want to escape apostrophes on the incoming request body properties, but I'm not confident this is the best way to do it.</p>
  
  <pre><code>exports.p_edit = function(req, res) {
  
      var args = [
          req.body.title.split("'").join("\\'"),
          req.files.thumbnail.name.split("'").join("\\'"),
          req.body.tags.split("'").join("\\'"),
          req.body.topic.split("'").join("\\'"),
          req.body.preview.split("'").join("\\'"),
          req.body.markdown.split("'").join("\\'")
      ];
      // ...
      // Do other stuff
  </code></pre>
  
  <p>I know I can use a <code>for(prop in req.body)</code> loop to iterate over the all request body properties, but how can I reassign the values dynamically to each property? </p>
  
  <p>For example if I do</p>
  
  <pre><code>for(prop in req.body) {
      prop.split("'").join("\\'");
  }
  </code></pre>
  
  <p>and later reference <code>req.body.title</code> (or any property), then the value is unchanged. Similarly, if I try to assign it like <code>req.body.prop = prop.split("'").join("\\'");</code> (hoping that <code>req.body.prop</code> would change dynamically with each iteration) it simply adds the <code>prop</code> property to the <code>req.body</code> object. How can I dynamically iterate and reassign these properties? </p>