How to Transform string message error in object or array?

Almir Júnior

I'm receiving this error response from an api:

[HTTP 400] - BAD_REQUEST 0 [53015] - sender name invalid value: almir1 [53044] - credit card holder name invalid value: almir

How could i transform this response to an object or array in javascript?

for example:

     var errors = [
        {key : 53015: message: "sender name invalid value"},
        {key : 53044: message: "credit card holder name invalid value: almir"}
    ];
Simone Bembi

This should do the trick:

const text = `[HTTP 400] - BAD_REQUEST 0 [53015] - sender name invalid 
value: almir1 [53044] - credit card holder name invalid value: almir`;
let regex = /\[([^\]]+?)\]\s\-\s([^\[]+)/g;
let matches;
const errors = {};
while (matches = regex.exec(text)) {
    errors[matches[1]] = matches[2];
}

Output:

{
    "53015": "sender name invalid value: almir1 ",
    "53044": "credit card holder name invalid value: almir",
    "HTTP 400": "BAD_REQUEST 0 "
}

To create a key/message array use this instead

const text = `[HTTP 400] - BAD_REQUEST 0 [53015] - sender name invalid value: almir1 [53044] - credit card holder name invalid value: almir`;
let regex = /\[([^\]]+?)\]\s\-\s([^\[]+)/g;
let matches;
const errors = [];
while (matches = regex.exec(text)) {
    errors.push({
        key: matches[1],
        message: matches[2]
    });
}

Output:

[
    {
        "key": "HTTP 400",
        "message": "BAD_REQUEST 0 "
    },
    {
        "key": "53015",
        "message": "sender name invalid value: almir1 "
    },
    {
        "key": "53044",
        "message": "credit card holder name invalid value: almir"
    }
]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to transform a string object into an array in ruby on rails?

How to transform an array to an object

How to transform Array to Object?

How to transform a string to an array

How transform object that contain arrays in keys to array of string with certain field?

how to to transform an object to a string php

How to transform string into object by Lodash

How to transform an object into an array of objects?

How to transform object into sorted array?

How to transform an array of object as array into array

How to transform array of object into more advanced array

How to transform Array[(String,List[String])] to Array[(String,String)]

How to transform a series object into a single string

How to transform this specific js array into js object?

How to transform nested object of objects into array of objects

How to transform an object into an array of objects by its keys?

how to transform array of object using reduce of map?

How to transform object map to array with Ramda?

How to use reduce to transform an array into an object?

Javascript, how to transform array row to object fields

How to transform an object to an array with nested objects?

NestJS: How to transform an array in a @Query object

How to transform JavaScript object values into array

Mongodb - How to transform nested object in an array

How to transform an array into an object with "n" arrays?

How can I transform the next object array?

how can i transform this object in to array?

How to transform Object into Array with key and values?

How to transform an object into an array of objects by keys?