0

This is the object:

const b = {1: [[0, 'a'], [0, 'b'], [0, 'c']], 2: [[1, 'a'], [1, 'b']], 3: [[2, 'a'], [2, 'b']]}

It has been sorted with bucket sort.

I need to create a sub array with two arrays, i.e.: { 1: [[[0, 'a'], [0, 'b']], [[0, 'c']]]

In order to do that I must iterate over the value.

Is there anyway to do it in linear time complexity?

My initial thoughts were to remove the value from the key, work on it on a separate loop and re-insert.

Madd World
  • 45
  • 5
  • Does it matter that these arrays are inside an object? Do you need to chunk all arrays in the object, or only a single one (`b[1]`)? – Bergi Jul 07 '20 at 11:06

1 Answers1

1

You can do it using Object.entries like the following:

var b = {1: [[0, 'a'], [0, 'b'], [0, 'c']], 2: [[1, 'a'], [1, 'b']], 3: [[2, 'a'], [2, 'b']]};
var result = {};
Object.entries(b).forEach(item => {
    result[item[0]] = [item[1]];
});
console.log(result);
Omid Nikrah
  • 2,444
  • 3
  • 15
  • 30