Convert Javascript string to Dictionary


Convert Javascript string to Dictionary



I'm trying to get Redis data in NodeJS. The Redis data needs to be converted to a dictionary for further processing. I gave it a try but finding difficult as parseFloat on string keeps giving me NaN.



Can anyone please help me to do it the correct way? Variable string has the sample data in below code. Please see the expected results below.




var string = "[ '[Timestamp('2018-06-29 15:29:00'), '260.20', '260.40', '260.15', '260.30']' ]";
string = string.replace("[", "");
string = string.replace("]", "");
string = string.replace("[", "");
string = string.replace("]", "");

s1 = string
var array = string.split(",");

var final = "{" + ""date"" + ":" + """ + array[0] + ","Open"" + ":" + """ + array[1].trim() + """ + ","High"" + ":" + """ + array[2].trim() + """ + ","Low"" + ":" + """ + array[3].trim() + """ + ","Close"" + ":" + """ + array[4].trim() + """ + "}";
console.log(final);



Expected Result:


{
"date": " Timestamp('2018-06-29 15:29:00')",
"Open": "260.20",
"High": "260.40",
"Low": "260.15",
"Close": "260.30"
}





What output does your code produce?
– BenM
Jun 30 at 5:58





@BenM Output of my code should be a dictionary which will be passed on to ag-grid
– user2371563
Jun 30 at 6:01





What does your current code produce?
– BenM
Jun 30 at 6:01





@BenM Here is my current output {"date":" 'Timestamp('2018-06-29 15:29:00'),"Open":"'260.20'","High":"'260.40'","Low":"'260.15'","Close":"'260.30''"}
– user2371563
Jun 30 at 6:02






You need to strip " chars from your values before passing them to parseFloat().
– BenM
Jun 30 at 6:05


"


parseFloat()




2 Answers
2



You have extra apostrophes at both sides of your string which is not needed and while parsing parseFloat("'260.20'")it returns NaN. You could remove them from the array as below:


parseFloat("'260.20'")


NaN


array = array.map( (str) => str.replace(/'/g, '') );

parseFloat(array[1]); // 260.20





Thank you for the solution!
– user2371563
Jun 30 at 6:18



It would probably be a whole lot easier to split the string by single-quotes, then combine into an object:


split




const string = "[ '[Timestamp('2018-06-29 15:29:00'), '260.20', '260.40', '260.15', '260.30']' ]";
const [,,timestamp,,Open,,High,,Low,,Close] = string.split("'");
const obj = {
date: `Timestamp('${timestamp}')`,
Open,
High,
Low,
Close
}
console.log(obj);



Note that ' inside a string delimited by double-quotes doesn't do anything - it's the same as '. (If you need a literal backslash, use )


'


'






Thank you very much!! Appreciate your help!
– user2371563
Jun 30 at 6:12






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV