I'm currently developing a function that accepts a list structured like this:
const payload = [
[1, 2],
[1,2,3],
{
name: 'sandra',
age: 20,
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ec9f8d82889e8dac949596c28f8381">[email protected]</a>'
}
]
and transforms it into an object like this:
{
name: 'return payload',
response: [
{ arr: [1, 2] },
{ arr: [1, 2, 3] },
{ arr: {
name: 'sandra',
age: 20,
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3e4d5f505a4c5f7e464744105d5153">[email protected]</a>'
}
]
}
This is the current version of my function:
const returnPayload = (arr) => {
let response = [];
for(let i = 0; i < arr.length; i++) {
response.push({arr: arr[i]})
}
return {"name": "return payload", "response": response}
}
returnPayload(payload);
console.log(returnPayload(payload))
However, when I run the function, it returns the following result:
{
name: 'return payload',
response: [ { arr: [Array] }, { arr: [Array] }, { arr: [Object] } ]
}
I've looked at various solutions online suggesting using JSON.stringify(obj)
to tidy up the output, but I prefer a simpler and more readable alternative. With JSON method, the output looks like this:
{"name":"return payload","response":[{"arr":[1,2]},{"arr":[1,2,3]},{"arr":{"name":"sandra","age":20,"email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e093818e849281a098999ace838f8d">[email protected]</a>"}}]}
Pardon the title, I'm not quite sure how to explain this issue.