1

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.

Fiyin Akinsiku
  • 166
  • 2
  • 13

2 Answers2

1

Sounds like you just need to map the array and return an object containing each item in the callback:

const payload = [
    [1, 2],
    [1,2,3],
    {
        name: 'sandra',
        age: 20,
        email: 'sandra@xyz.com'
    }
];

const result = {
  name: 'return payload',
  response: payload.map(item => ({ arr: item }))
};
console.log(result);

(but the arr property is not necessarily an array, as you can see by the 3rd item - consider a more informative property name, perhaps?)

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1

if by "easy-to-read" you mean how to see the result, you can continue with JSON.stringify() but add an additional argument for spacing.

const payload = [
    [1, 2],
    [1,2,3],
    {
        name: 'sandra',
        age: 20,
        email: 'sandra@xyz.com'
    }
]

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(JSON.stringify(returnPayload(payload), null, 3))
charmful0x
  • 155
  • 9
  • Yeah this works but I will be using a larger array and the return value will be a lot more than what is in my question. I'm looking for a way to make the results look like this: `{ arr: [1, 2, 3], arr: [1,2,3,4] }`. So it's readable – Fiyin Akinsiku May 01 '21 at 02:05
  • if you only want to read the `arr` key's value, why not adding a `console.log(JSON.stringify({arr: arr[i]}))` in the for-loop? – charmful0x May 01 '21 at 02:13
  • I'm not just trying to read the key values, I want them to be returned when the function is called. But I think I'll just go with the `JSON.stringify`method since it works. – Fiyin Akinsiku May 01 '21 at 02:23