I have an array of strings like this:
const array = [
"date: 1679534340367, price: 27348.6178237571831766",
"date: 1679534340367, price: 27348.6178237571831766",
"date: 1679534340367, price: 27348.6178237571831766",
"date: 1679534340367, price: 27348.6178237571831766",
"date: 1679534340367, price: 27348.6178237571831766"
]
How can i convert it to an array of objects to look like this:
const array = [
{"date": 1679534340367, "price": 27348.6178237571831766},
{"date": 1679534340367, "price": 27348.6178237571831766},
{"date": 1679534340367, "price": 27348.6178237571831766},
{"date": 1679534340367, "price": 27348.6178237571831766},
{"date": 1679534340367, "price": 27348.6178237571831766},
]
2
Answers
There are several options, the best in my opinion is using regular expressions:
In addition, you can also use a map and split:
You can use
.replace
with regexp to escape object keys, wrap it with{}
, and convert to objects usingJSON.parse()
Or without intermediate steps: