1

I got problem, I've array of string as

[
    "Time:25/10/2019 14:49:47.41,Server:Daniel.Europe.A…itical,Area:Europe,Site:,Station:Aberdeen,Stream:", 
    "Time:25/10/2019 14:49:48.16,Server:Daniel.Europe.U…,Area:Europe,Site:United Kingdom,Station:,Stream:"
]

I need to convert it to Object

[
    {"Time" : "25/10/2019 14:49:47.41", "Server", "Daniel.Europe..", .. },
    {}
]

likewise.

JSON.parse won't work on non-serialized string.

5
  • try JSON.stringify(yourArray); Commented Oct 30, 2019 at 13:19
  • 1
    Either change the process that sends you the data in that format (best option) so it comes in proper JSON format or figure out how to parse it yourself. Commented Oct 30, 2019 at 13:19
  • Split each string by , and then each of those strings by (the first) :. Commented Oct 30, 2019 at 13:19
  • An array of string? You basically have one string in an array? Look at @crahsmstr's comment. The essence of the problem is probably in the API/Back-end. Commented Oct 30, 2019 at 13:21
  • @crashmstr I understand it is problem in backend/api, but I can't change it.. thanks for suggestion Commented Oct 30, 2019 at 13:26

4 Answers 4

3

Using Object.fromEntries()

var data = [
  "Time:25/10/2019 14:49:47.41,Server:Daniel.Europe.A…itical,Area:Europe,Site:,Station:Aberdeen,Stream:",
  "Time:25/10/2019 14:49:48.16,Server:Daniel.Europe.U…,Area:Europe,Site:United Kingdom,Station:,Stream:"
]

var result = data.map(v => 
  Object.fromEntries(v.split(',').map(v => v.split(/:(.*)/)))
)

console.log(result)

Sign up to request clarification or add additional context in comments.

1 Comment

But... is less browser compatible than reduce: caniuse.com/#search=fromEntries not IE/Edge support
1

Something like this should work:

input.map(v => v.split(',').map(v => {
    const [key, ...value] = v.split(':');
    const obj = {};
    obj[key] = value.join(':');
    return obj;
}))

3 Comments

careful. split() will discard your time string after the first colon. See @User863 answer.
@mankowitz That doesn't appear to be the case in my testing. That would only happen if you supplied 2 as the 2nd argument to .split
@DanielSamuels Ohhh. I had used the 2nd argument in my code. You're right.
0

You can get it using map and reduce:

const arr = [
    "Time:25/10/2019 14:49:47.41,Server:Daniel.Europe.A…itical,Area:Europe,Site:,Station:Aberdeen,Stream:", 
    "Time:25/10/2019 14:49:48.16,Server:Daniel.Europe.U…,Area:Europe,Site:United Kingdom,Station:,Stream:"
]

const newArr = arr.map(item => {
return item.split(",").reduce((acc, curr) => {  
  const label = curr.split(":")[0];
  const value = curr.substring(label.length+1)
  acc[curr.split(":")[0]] = value
  return acc;
},{})
})

console.log(newArr)

Comments

0

You have to split your strings by commas and colons. Only problem is that your time string has a bunch of colons in it. Here is a start.

var a = [
    "Time:25/10/2019 14:49:47.41,Server:Daniel.Europe.A…itical,Area:Europe,Site:,Station:Aberdeen,Stream:", 
    "Time:25/10/2019 14:49:48.16,Server:Daniel.Europe.U…,Area:Europe,Site:United Kingdom,Station:,Stream:"
];

b = a.map(function(line) {
  var obj = {};
  line.split(",").forEach(function(item) {
    kv = item.split(/:(.+)/,2);
    obj[kv[0]]=kv[1];
  });
  return obj;
});

console.log(b);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.