Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

split and convert a string into object

let data = "Child 1 First Name: Ali
Child 1 Gender: Female
Child 1 Hair Color: Blonde
Child 1 Hair Style: Wavy
Child 1 Skin Tone: Tan
Child 2 First Name: Morgan 
Child 2 Gender: Female
Child 2 Hair Color: Brown
Child 2 Hair Style: Ponytail
Child 2 Skin Tone: Light
Relationship 1 to 2: Brother
Relationship 2 to 1: Brother
";
//let data = JSON.stringify(rawNoteData);  <-- Don't do this. order.customer.note is not an object.

let foo = data.split("
").reduce(function(obj, str, index) {
  let strParts = str.split(":");
  if (strParts[0] && strParts[1]) { //<-- Make sure the key & value are not undefined
    obj[strParts[0].replace(/s+/g, '')] = strParts[1].trim(); //<-- Get rid of extra spaces at beginning of value strings
  }
  return obj;
}, {});

console.log(foo);
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript string error 
Javascript :: javascript round to nearest integer 
Javascript :: react big calendar messages 
Javascript :: get current tab from chrome extension developer 
Javascript :: array.splice javascript 
Javascript :: javascript trim whitespace 
Javascript :: how to get csrf token in javascript 
Javascript :: json vs xml 
Javascript :: nodejs postgresql local connection 
Javascript :: javascript close app phonegap 
Javascript :: update in mongoose node js 
Javascript :: chrome.storage.local delete 
Javascript :: javascript window screen 
Javascript :: add parameter at the end of url from jquery with refreshing 
Javascript :: js overflowy 
Javascript :: how to create an element in js using the map method 
Javascript :: get data from google sheets javascript 
Javascript :: check empty object javascript 
Javascript :: difference between normal function and arrow function 
Javascript :: replacing a value in string using aregular expression pyhton 
Javascript :: md5 checksum javascript 
Javascript :: react materialize cdn 
Javascript :: fingerprint js 
Javascript :: new line in javascript string 
Javascript :: javascript tostring 
Javascript :: js delete all cookies 
Javascript :: how to get value inside span using javascript 
Javascript :: how to print something in javascript 
Javascript :: how to pass sequelize transaction to save method 
Javascript :: Remove items from an index position 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =