2

I have a String message like that :

messages = "Line 249 : Validation error, Line 287 : Validation error"

I want to split this message like this :

messages [] = [ { position: 1, message: 'Line 249 : Validation error' },

{ position: 2, message: 'Line 287 : Validation error' }]

Could you please help with this thank you.

2
  • What you have tried so far? Commented Aug 12, 2020 at 22:15
  • @Ilyas Khallouq I added some more relevant tags than angular for you. That should get you an answer faster. Commented Aug 12, 2020 at 22:24

2 Answers 2

3

The easiest way to turn a string into an array of objects is to first split the string using the delimiter, which in this case is the comma, so start with.

const test = "Line 249 : Validation error, Line 287 : Validation error";
const parts = test.split(",");

Then, you want to use the map array function to return an object for each part that's been split. The es6 map function has a callback that returns the piece of the array and the index in which it was found. you don't want the index, but rather an ordinal (per your example above)

Here's what i would do:

const test = "Line 249 : Validation error, Line 287 : Validation error"; 
const parts = test.split(",").map((text, index) => {
  return {
    position: index+1,
    message: text.trim()
  }
});

Now, the parts variable holds an array of objects that matches your required output

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

1 Comment

Was looking for a way to allow user to input comma separated values and make it a string array for model member. No direct mapping from ngModel to that object member but then, I created a component member of type string which represents the user input, then on the click of the save button, I made myself a function that splits and maps the string directly to a string array: var stringArray=test.split(',').map(text => { return text.trim() });. Let's hope this helps one.
3

I think your error message can be split into an array using javascript split method. so

messages = message.split(',') 

will do the magic But to add your position,

let messages = "Line 249 : Validation error, Line 287 : Validation error"

messages= messages.split(',').map((x,index)=>{
    let obj ={}
     obj.position=index+1;
     obj.message = x
     return obj;
 });
 
 console.log( messages )

3 Comments

That would create an array of strings, not an array of objects.
Hi when i do this i got the following result : Line 429: the studentid must not be empty,Line 430: the studentid must not be empty . i want someting like this : messages [] = [ { position: 1, message: 'Line 249 : Validation error' }, { position: 2, message: 'Line 287 : Validation error' }]
I turned your code in an interactive snippet. You may want to trim() the message string as well. That aside it looks like a good answer :).

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.