Hey, Im trying to get a response here of JSON array in a single line but it is giving in multi line array. Can someone help me with it?
The is the response im getting
{
“id_success”: true,
“user_id”: “”,
“email”: “”,
“roll_number”: “”,
“numbers”: ,
“alphabets”: [
“A”,
“C”,
“Z”,
“c”,
“i”
],
“highest_lowercase_alphabet”: “i”
}
This is the response I want
{
“id_success”: true,
“user_id”: “”,
“email”: “”,
“roll_number”: “”,
“numbers”: ,
“alphabets”: [“A”,“C”,“Z”,“c”,“i”],
“highest_lowercase_alphabet”: “i”
}
This is a post request , Im sending this data to the request
{
“data”: [“A”, “C”, “Z”, “c”, “i”]
}
This is my backend code
export async function POST(request: Request) {
const requestBody = await request.json();
const iterableArray: string[] = requestBody.data ?? [];
const numsArray: number[] = [];
const letterArray: string[] = [];
let highestLowercaseLetter = '';
iterableArray.forEach((item: string) => {
if (!isNaN(Number(item)) && item.trim() !== '') {
numsArray.push(Number(item));
}
if (/^[A-Z]$/.test(item) || /^[a-z]$/.test(item)) {
letterArray.push(item);
if (/^[a-z]$/.test(item)) {
if (highestLowercaseLetter === '' || item > highestLowercaseLetter) {
highestLowercaseLetter = item;
}
}
}
});
const responseJson = {
id_success: true,
user_id: "",
email: "",
roll_number: "",
numbers: numsArray,
alphabets: letterArray,
highest_lowercase_alphabet: highestLowercaseLetter
};
// Remove the formatting argument to get the array in a single line
const responseJsonFormat = JSON.stringify(responseJson);
return new NextResponse(responseJsonFormat, {
status: 200,
headers: {
'Content-Type': 'application/json'
}
});
}