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!

lecham
  • 1,450
  • 4
  • 14
  • 27

4 Answers4

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);
jmtalarn
  • 1,255
  • 1
  • 12
  • 14
  • The function `flat` adds overhead in this approach. – Ele Feb 10 '20 at 11:31
  • 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. – jmtalarn Feb 10 '20 at 11:39
  • 1
    Got it, you kept it that way just to let it know how to use the function `flat`. – Ele Feb 10 '20 at 11:41
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; }
Ele
  • 31,191
  • 6
  • 31
  • 67
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);
Mamun
  • 58,653
  • 9
  • 33
  • 46
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);
Mohammad Faisal
  • 1,230
  • 8
  • 19