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: '[email protected]'
    }
]

and returns an object like this:

{
  name: 'return payload',
  response: [ 
    { arr: [1, 2] }, 
    { arr: [1, 2, 3] }, 
    { arr: {
      name: 'sandra',
      age: 20,
      email: '[email protected]'
    }
  ]
}

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":"[email protected]"}}]}

Pls forgive the title, I'm not sure how to describe this issue.

2 Answers 2

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: '[email protected]'
    }
];

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?)

Sign up to request clarification or add additional context in comments.

11 Comments

Yeah, this is just a sample, not the original function. I use a more descriptive name in the main code.
For some reason, your suggestion runs fine here, but not on vscode or repl.
I just tried it in VSCode and it ran just fine. What exactly is the problem you're running into?
Hmm, that's strange. On vscode, I am still getting the same response format as in my question: {name: 'return payload', response: [ { arr: [Array] }, { arr: [Array] }, { arr: [Object] } ]}.
See stackoverflow.com/questions/10729276/… though if I were you, I wouldn't bother with trying to get sconsole.log`s to display prettily, just do what you need to do with the data instead, and it'll work
|
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: '[email protected]'
    }
]

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))

3 Comments

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
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?
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.stringifymethod since it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.