Javascript - Iterate over deep nested object and change certain unknown values
<p>I have an object with an unknown number of levels and types (array, object, string), here is an example:</p>

<pre><code>var data = {
    ffs: false,
    customer: {
        customer_id: 1544248,
        z_cx_id: '123456',
    },
    selected_items: {
        '3600196': [{
            id: 4122652,
            name: 'Essential Large (up to 8\'x10\')',
            selected: true
        }]
    },
    service_partner: {
        id: 3486,
        name: 'Some String',
        street: '1234 King St.',
    },
    subject: 'Project-2810191 - Orange Juice Stain (Rug)',
    description: 'Product Type: \n\nIssue: (copy/paste service request details here)\n\nAction Required:',
}
</code></pre>

<p>I have to loop through every property using plain javascript and sanitize each string that has a ' to have '' (for use in a PostGres Query), I change it with the following code:</p>

<pre><code>val = val.replace(/'/g, "''");
</code></pre>

<p>And I can loop through the object and display them with this, although this may not be the best solution:</p>

<pre><code>function iterObj(obj) {
  for (var key in obj) {
    console.log(key + ': ' + obj[key]);
    if (obj[key] !== null &amp;&amp; typeof obj[key] === "object") {
      // Recurse into children
      iterObj(obj[key]);
    }
  }
}

iterObj(data);
</code></pre>

<p>The problem is that I don't know how to actually do the sanitization part and update the original data array since this is going in circles.</p>

<p>I found plenty of people asking similar questions but couldn't quite get their answers to work in my case.</p>

<p>Any help is appreciated.</p>