-7

How would I split the following array using javascript? what I want to do here is split this array into multiple arrays inside this array which should be unique by 1, 2, 3, 4, 5 candidateConfigId

0: {candidateConfigId: "1", value: "199128700790"}
1: {candidateConfigId: "2", value: "Yujith"}
2: {candidateConfigId: "3", value: "Male"}
3: {candidateConfigId: "4", value: "SE"}
4: {candidateConfigId: "5", value: "Colombo5"}
5: {candidateConfigId: "1", value: "199128700792"}
6: {candidateConfigId: "2", value: "Yujith2"}
7: {candidateConfigId: "3", value: "Male"}
8: {candidateConfigId: "4", value: "SE"}
9: {candidateConfigId: "5", value: "Colombo3"}
10: {candidateConfigId: "1", value: "199128700793"}
11: {candidateConfigId: "2", value: "Yujith3"}
12: {candidateConfigId: "3", value: "Male"}
13: {candidateConfigId: "4", value: "SE"}
14: {candidateConfigId: "5", value: "Colombo2"}

Expected output

[
  [
    0: {candidateConfigId: "1", value: "199128700790"}
    1: {candidateConfigId: "2", value: "Yujith"}
    2: {candidateConfigId: "3", value: "Male"}
    3: {candidateConfigId: "4", value: "SE"}
    4: {candidateConfigId: "5", value: "Colombo5"}
  ],
  [
    0: {candidateConfigId: "1", value: "199128700792"}
    1: {candidateConfigId: "2", value: "Yujith2"}
    2: {candidateConfigId: "3", value: "Male"}
    3: {candidateConfigId: "4", value: "SE"}
    4: {candidateConfigId: "5", value: "Colombo3"}
  ],
  [
    10: {candidateConfigId: "1", value: "199128700793"}
    11: {candidateConfigId: "2", value: "Yujith3"}
    12: {candidateConfigId: "3", value: "Male"}
    13: {candidateConfigId: "4", value: "SE"}
    14: {candidateConfigId: "5", value: "Colombo2"}
  ]
]
 Thanks in advance
Ujith Isura
  • 67
  • 2
  • 10

3 Answers3

1

You could take an array of the wanted ids and an object which stores the index for the result set for a same group.

var data = [{ candidateConfigId: "1", value: "199128700790" }, { candidateConfigId: "2", value: "Yujith" }, { candidateConfigId: "3", value: "Male" }, { candidateConfigId: "4", value: "SE" }, { candidateConfigId: "5", value: "Colombo5" }, { candidateConfigId: "1", value: "199128700792" }, { candidateConfigId: "2", value: "Yujith2" }, { candidateConfigId: "3", value: "Male" }, { candidateConfigId: "4", value: "SE" }, { candidateConfigId: "5", value: "Colombo3" }, { candidateConfigId: "1", value: "199128700793" }, { candidateConfigId: "2", value: "Yujith3" }, { candidateConfigId: "3", value: "Male" }, { candidateConfigId: "4", value: "SE" }, { candidateConfigId: "5", value: "Colombo2" }],
    ids = ['1', '2', '3', '4', '5'],
    result = data
        .reduce((r, o) => {
            let index = r.indices[o.candidateConfigId]++;
            r.data[index] = r.data[index] || [];
            r.data[index].push(o);
            return r;
        }, { indices: Object.fromEntries(ids.map(k => [k, 0])), data: [] })
        .data;

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • @OP just so you understand what this does (differently), it allows your ids to be completely out of order. Meaning you could have `[{id:1},{id:1},{id:2},{id:1},{id:3}...]` and it would still chunk unique ids into arrays `[ [{id:1},{id:2},{id:3}...], [{id:1},...], [{id:1}...] ]`. – user120242 Jun 30 '20 at 09:14
0

Say this:

0: {candidateConfigId: "1", value: "199128700790"}
1: {candidateConfigId: "2", value: "Yujith"}
2: {candidateConfigId: "3", value: "Male"}
3: {candidateConfigId: "4", value: "SE"}
4: {candidateConfigId: "5", value: "Colombo5"}
5: {candidateConfigId: "1", value: "199128700792"}
6: {candidateConfigId: "2", value: "Yujith2"}
7: {candidateConfigId: "3", value: "Male"}
8: {candidateConfigId: "4", value: "SE"}
9: {candidateConfigId: "5", value: "Colombo3"}
10: {candidateConfigId: "1", value: "199128700793"}
11: {candidateConfigId: "2", value: "Yujith3"}
12: {candidateConfigId: "3", value: "Male"}
13: {candidateConfigId: "4", value: "SE"}
14: {candidateConfigId: "5", value: "Colombo2"}

is this var: array and is ALWAYS sorted like in the posted example (thats the tricky part). In this case, you can use a reduce function like this:

let currentIndex = 0;
const Splitted = array.reduce((acc, cur) => {
             acc[currentIndex].push(cur);

            if (parseInt(cur.candidateConfigId) === 5) { currentIndex++; acc[currentIndex] = [] }
            return acc;
},[[]])
Raffobaffo
  • 2,806
  • 9
  • 22
0
var subject = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];

function chunksOfSize(source, size) {
  var arr = [];
  for (var i = 0, count = source.length; i < count; i += size) {
    arr.push(arr.slice(i, i + size));
  }
  return arr;
}

console.log(chunksOfSize(subject, 4));
Ovais
  • 374
  • 2
  • 7