I am working on a function that takes in a list like this:
const payload = [
[1, 2],
[1,2,3],
{
name: 'sandra',
age: 20,
email: 'sandra@xyz.com'
}
]
and returns an object like this:
{
name: 'return payload',
response: [
{ arr: [1, 2] },
{ arr: [1, 2, 3] },
{ arr: {
name: 'sandra',
age: 20,
email: 'sandra@xyz.com'
}
]
}
This is the function I have:
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))
and this is what it currently returns:
{
name: 'return payload',
response: [ { arr: [Array] }, { arr: [Array] }, { arr: [Object] } ]
}
I have checked several solutions online and the recommendations are to pass the returning object into JSON.stringify(obj)
, but I am looking for a neater and easier-to-read alternative. The JSON method returns this:
{"name":"return payload","response":[{"arr":[1,2]},{"arr":[1,2,3]},{"arr":{"name":"sandra","age":20,"email":"sandra@xyz.com"}}]}
Pls forgive the title, I'm not sure how to describe this issue.