1

What is the best way to convert

const mockResults = [
    [{ user: { firstName: '1', lastName: '1' }, status: 'WRONG' }],
    [{ user: { firstName: '2',lastName: '2' }, status: 'WRONG' }],
    [{ user: { firstName: '3',lastName: '3' }, status: 'CORRECT' }]
];

to

const mockResults = [
    { user: { firstName: '1', lastName: '1' }, status: 'WRONG' },
    { user: { firstName: '2',lastName: '2' }, status: 'WRONG' },
    { user: { firstName: '3',lastName: '3' }, status: 'CORRECT' }
];

The whole task is to transform mockResults to requiredFormat, that's why I need to remove nested arrays:

const requiredFormat = [
  {
    status: 'WRONG',
    data: [{ user: {firstName: '1', lastName: '1'}}, { user: {firstName: '2', lastName: '2'}}],
  },
  {
    status: 'CORRECT',
    data: [{ user: {firstName: '3', lastName: '3'}}],
  },
];

Here's what I tried so far: https://jsfiddle.net/9uLje3sg/

Thanks!

7
  • What have you tried so far? Have you considered using .map? Commented Feb 10, 2020 at 10:46
  • 1
    mockResults.map(i => i[0]) Commented Feb 10, 2020 at 10:46
  • Does this answer your question? Merge/flatten an array of arrays Commented Feb 10, 2020 at 10:47
  • let result = mockResults.map(([user]) => user); Commented Feb 10, 2020 at 10:48
  • @evolutionxbox Update the question with a demo Commented Feb 10, 2020 at 10:52

4 Answers 4

4

You can use flat method from Array javascript object. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat

If you want to change the source of data and change the shape of it, using map and reduce methods can help you.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

In your precise example reduce would fit as you are creating a new object grouping per status property.

const mockResults = [
    [{ user: { firstName: '1', lastName: '1' }, status: 'WRONG' }],
    [{ user: { firstName: '2',lastName: '2' }, status: 'WRONG' }],
    [{ user: { firstName: '3',lastName: '3' }, status: 'CORRECT' }]
];
const flattedAndReduced = mockResults.flat().reduce( (acc, curr)=>{
  const statusIndex = { 'WRONG' : 0, 'CORRECT': 1 };
  acc[statusIndex[curr.status]].data.push({ user: curr.user} );
  return acc;
}, [
    {
      status: 'WRONG',
      data: [],
    },
    {
      status: 'CORRECT',
      data: [],
    }
  ]
  );
console.log(flattedAndReduced);

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

3 Comments

The function flat adds overhead in this approach.
That's true, any reiteration over the data source does that. Originally the user, just asked about how to "flat" the array and as he kept the question but added a "second step" I updated my original answer too.
Got it, you kept it that way just to let it know how to use the function flat.
2

Use the function map as follow which returns an array with the desired objects.

let result = mockResults.map(([user]) => user);

That approach is assuming there is only one index per array from the original array.

According to the approach for requiredFormat

You can use the function reduce for grouping and the function Object.values for getting the desired output.

const mockResults = [
    [{ user: { firstName: '1', lastName: '1' }, status: 'WRONG' }],
    [{ user: { firstName: '2',lastName: '2' }, status: 'WRONG' }],
    [{ user: { firstName: '3',lastName: '3' }, status: 'CORRECT' }]
];

let requiredFormat = Object.values(mockResults.reduce((a, [{user, status}]) => {
  (a[status] || (a[status] = {data: [], status})).data.push(user);
  return a;
}, Object.create(null)));

console.log(requiredFormat);
.as-console-wrapper { max-height: 100% !important; top: 0; }

1 Comment

Thanks! I got it. Sorry for misunderstanding!
1

Simply use Array.prototype.map() to return the object from first index.

Please Note: variable declared with const can not be modified, use *let instead.

let mockResults = [
    [{ user: { firstName: '1', lastName: '1' }, status: 'WRONG' }],
    [{ user: { firstName: '2',lastName: '2' }, status: 'WRONG' }],
    [{ user: { firstName: '3',lastName: '3' }, status: 'CORRECT' }]
];

mockResults = mockResults.map(i => i[0]);
console.log(mockResults);

Comments

0

const mockResults = [
    [{ user: { firstName: '1', lastName: '1' }, status: 'WRONG' }],
    [{ user: { firstName: '2',lastName: '2' }, status: 'WRONG' }],
    [{ user: { firstName: '3',lastName: '3' }, status: 'CORRECT' }]
];
const requiredFormat = [
  {status: 'WRONG', data: []},
  {status: 'CORRECT', data: []},
];

for(let [{user,status}] of mockResults) {  
  requiredFormat[ status==="WRONG" ? 0 : 1].data.push({user});
}
console.log(requiredFormat);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.