-3

This is with respect to the following

Transform string of objects into elements of object

As per that, i had a different data. Instead of that data, i have comma seperated values. How can i put all those values in element. Any Suggestion please.

Here is my new data.

[0,5]
0: "A123,G,2323232"
1: "F345,G,345667"
2: "T677,G,-34343"
3: "G454,G,4343"
4: ""

Can someone please let me know how to convert above commas values present in an array into the following

[0,4]
0: 
 UserId:A123
 Type:  G
 Values: 2323232
1: 
 UserId: F345  
 Type:  G
 Values: 345667
2: 
 UserId: T677  
 Type:  G
 Values: -34343
3: 
 UserId: G454  
 Type:  G
 Values: 4343
1
  • I think there's a problem with the data structure that you are trying to handle. using Regex as @Kyon can solved the problem, but I think you should formalize the data model that used here. Commented Aug 28, 2017 at 21:24

1 Answer 1

4

var data = ["A123,G,2323232", "F345,G,345667", "T677,G,-34343", "G454,G,4343", ""],
    result = data
        .filter(Boolean)
        .map(s => {
            var [UserId, Type, Values] = s.match(/[^,]+/g);
            return { UserId, Type, Values };
        });
                
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

1 Comment

@Rihana don't forget to mark this as solved if your problem is solved with this code snippet.

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.