title: JavaScript Split String into Object with Properties
body: <p>I am having an issue where an API is giving me a long string that contains multiple items that need to be broken out into an object. Here is an example of the string: </p>
  
  <pre><code>"Child 1 First Name: Ali\nChild 1 Gender: Female\nChild 1 Hair Color: Blonde\nChild 1 Hair Style: Wavy\nChild 1 Skin Tone: Tan\nChild 2 First Name: Morgan \nChild 2 Gender: Female\nChild 2 Hair Color: Brown\nChild 2 Hair Style: Ponytail\nChild 2 Skin Tone: Light\nRelationship 1 to 2: Brother\nRelationship 2 to 1: Brother\n"
  </code></pre>
  
  <p>It is using <code>\n</code> to break everything into multiple lines. The string is one property of a larger object being sent by the API. For some reason when I try and use the <code>.split</code> method on it like (full code below) I need to use <code>JSON.stringify</code> in order to get my function to even begin to work. </p>
  
  <p>Here is my full code: </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>let rawNoteData = order.customer.note;
  let data = JSON.stringify(rawNoteData);
  
  let foo = data.split("\n").reduce(function(obj, str, index) {
    let strParts = str.split(":");
    obj[strParts[0].replace(/\s+/g, '')] = strParts[1];
    return obj;
  }, {});
  
  console.log(foo);</code></pre>
  </div>
  </div>
  </p>
  
  <p>This is creating really crazy looking objects that look like this: </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>{'"Child1FirstName': ' Arabelle\\nChild 1 Gender' }</code></pre>
  </div>
  </div>
  </p>
  
  <p>I think my function is only working for the first instance. I am a little uncertain on how to fix this and clean up all of the crazy quotes etc going on. </p>
  
  <p>My goal is to split the string into an object like so: </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>mongoExDoc: {
    child1FirstName: "Ali",
    child1Gender: "Female",
    child1HairColor: "Blonde",
    child1HairStyle: "Wavy",
    child1SkinTone: "Tan",
    child2FirstName: "Morgan",
    child2Gender: "Female",
    child2HairColor: "Brown",
    child2HairStyle: "Ponytail",
    child2SkinTone: "Light",
    relationship1To2: "Brother",
    relationship2To1: "Brother"
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>From there I will be combining its properties with another object being inserted into MongoDB. </p>
  
  <p>Any help is GREATLY appreciated! </p>