0

⛰️ Hi community,

I need to group an array of objs by their boolean

Data

In these simplify example i have data (an array of objs) where every objet has a boolean and i need to group it by the information in the boolean.

  • "R" = Right
  • "L" = Left

Data

 const data = 
   [
    {id: 3337, score: 75, date: '2000-02-02T07:25:00.000Z', boolean: "R"},
    {id: 4337, score: 65, date: '2000-02-02T07:25:15.000Z', boolean: "L"},
    {id: 3336, score: 30, date: '2012-06-02T08:24:00.000Z', boolean: "R"},
    {id: 4336, score: 32, date: '2012-06-02T08:24:15.000Z', boolean: "L"},
    {id: 3335, score: 25, date: '2020-08-02T09:23:00.000Z', boolean: "R"},
    {id: 4335, score: 30, date: '2020-08-02T09:23:15.000Z', boolean: "L"},
    {id: 2234, score: 85, date: '2018-08-02T12:20:00.000Z', boolean: "R"},
    {id: 4234, score: 80, date: '2018-08-02T12:20:12.000Z', boolean: "L"},
    {id: 1534, score: 85, date: '2016-08-02T10:30:00.000Z', boolean: "R"},
    {id: 4534, score: 88, date: '2016-08-02T10:30:15.000Z', boolean: "L"},
    {id: 3884, score: 85, date: '2019-08-02T11:18:00.000Z', boolean: "R"},
    {id: 4884, score: 79, date: '2019-08-02T11:18:15.000Z', boolean: "L"},
    {id: 6534, score: 25, date: '2012-08-02T13:01:00.000Z', boolean: "R"},
    {id: 4534, score: 22, date: '2012-08-02T13:05:00.000Z', boolean: "L"}
   ],

Expected Output

 const result =
 [
   [
    {id: 3337, score: 75, date: '2000-02-02T07:25:00.000Z', boolean: "R"},
    {id: 3336, score: 30, date: '2012-06-02T08:24:00.000Z', boolean: "R"},
    {id: 3335, score: 25, date: '2020-08-02T09:23:00.000Z', boolean: "R"},
    {id: 2234, score: 85, date: '2018-08-02T12:20:00.000Z', boolean: "R"},
    {id: 1534, score: 85, date: '2016-08-02T10:30:00.000Z', boolean: "R"},
    {id: 3884, score: 85, date: '2019-08-02T11:18:00.000Z', boolean: "R"},
    {id: 6534, score: 25, date: '2012-08-02T13:01:00.000Z', boolean: "R"}
   ],
   [
    {id: 4337, score: 65, date: '2000-02-02T07:25:15.000Z', boolean: "L"},
    {id: 4336, score: 32, date: '2012-06-02T08:24:15.000Z', boolean: "L"},
    {id: 4335, score: 30, date: '2020-08-02T09:23:15.000Z', boolean: "L"},
    {id: 4234, score: 80, date: '2018-08-02T12:20:12.000Z', boolean: "L"},
    {id: 4534, score: 88, date: '2016-08-02T10:30:15.000Z', boolean: "L"},
    {id: 4884, score: 79, date: '2019-08-02T11:18:15.000Z', boolean: "L"},
    {id: 4534, score: 22, date: '2012-08-02T13:05:00.000Z', boolean: "L"}
   ]
 ]

I think the answer would be with a map, sort it out and the push it, right? . But i don't have any clue how to do it.

Thanks a lot !

pilchard
  • 12,414
  • 5
  • 11
  • 23
Momo
  • 67
  • 7
  • Please visit [help], take [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output, preferably in a [Stacksnippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) – mplungjan Oct 20 '22 at 15:21
  • 1
    also: [Dividing an array by filter function](https://stackoverflow.com/questions/11731072/dividing-an-array-by-filter-function) – pilchard Oct 20 '22 at 15:29
  • 1
    @mplungjan regardless it is a duplicate many times over, and I'd probably use a `for...of` and avoid the multiple filter calls. – pilchard Oct 20 '22 at 15:30
  • 1
    `const result = [[],[]]; const test = ["L","R"]; data.forEach(item => result[test.findIndex(t => t === item.boolean)].push(item));` perhaps? – mplungjan Oct 20 '22 at 15:37

1 Answers1

3

You can use array.forEach() and test the value of your boolean, then put it in an array accordingly.

const result = [
   [],
   []    
]

data.forEach(row => {
    if(row.boolean === "L") result[0].push(row);
    if(row.boolean === "R") result[1].push(row);
});

console.log(result);
<script>

const data = 
   [
    {id: 3337, score: 75, date: '2000-02-02T07:25:00.000Z', boolean: "R"},
    {id: 4337, score: 65, date: '2000-02-02T07:25:15.000Z', boolean: "L"},
    {id: 3336, score: 30, date: '2012-06-02T08:24:00.000Z', boolean: "R"},
    {id: 4336, score: 32, date: '2012-06-02T08:24:15.000Z', boolean: "L"},
    {id: 3335, score: 25, date: '2020-08-02T09:23:00.000Z', boolean: "R"},
    {id: 4335, score: 30, date: '2020-08-02T09:23:15.000Z', boolean: "L"},
    {id: 2234, score: 85, date: '2018-08-02T12:20:00.000Z', boolean: "R"},
    {id: 4234, score: 80, date: '2018-08-02T12:20:12.000Z', boolean: "L"},
    {id: 1534, score: 85, date: '2016-08-02T10:30:00.000Z', boolean: "R"},
    {id: 4534, score: 88, date: '2016-08-02T10:30:15.000Z', boolean: "L"},
    {id: 3884, score: 85, date: '2019-08-02T11:18:00.000Z', boolean: "R"},
    {id: 4884, score: 79, date: '2019-08-02T11:18:15.000Z', boolean: "L"},
    {id: 6534, score: 25, date: '2012-08-02T13:01:00.000Z', boolean: "R"},
    {id: 4534, score: 22, date: '2012-08-02T13:05:00.000Z', boolean: "L"}
   ]
</script>
Arthur
  • 345
  • 4
  • 10
  • Cool thanks a lot @Arthur.. I have a question in your code. Why do put row.boolean or push(row).. what is row? – Momo Oct 21 '22 at 08:17
  • 1
    the forEach statement allows you to loop over each element of your array, and apply a function to each of these elements. This function takes a parameter (the current element of the array). I named it row. It is equivalent to for(let i = 0; i < data.length; i++) { row = data[i]; ... } ! – Arthur Oct 21 '22 at 08:40