Javascript Object of Arrays manipulation I am writing a function to decode data from a run length encoded format like: 


{
  dataGroup: {
    count: [4, 4, 2, 1],
      dataOne: [1, 3, 2, 1],
      dataTwo: [7, -1, 9, 0],
      dataThree: [3, 8, 1, 2]
  }
}



to 

{
  groupName: {
    dataOne:   [1, 1, 1, 1, 3, 3, 3, 3, 2, 2, 1],
    dataTwo:   [7, 7, 7, 7,-1,-1,-1,-1, 9, 9, 0],
    dataThree: [3, 3, 3, 3, 8, 8, 8, 8, 1, 1, 2]
  }
}


I have written two solutions to date but feel like there may yet be a better way.

function decode(obj) {

    const arrayLengthN = (N, data=null) =&gt; [...Array(N)].map(_ =&gt; data);

    const [ group ] = Object.keys(obj);
    const { count, ...data } = obj[group];
    const dataKeys = Object.keys(data);

    // method one

    // output object to be written upon iteration over count and data
    const output = {};

    // populate output with empty arrays to push to
    dataKeys.map(key =&gt; output[key] = []);

    // do the decoding
    count
        .map((n, i) =&gt; {
            dataKeys
                .map(key =&gt; {
                    output[key].push(...arrayLengthN(n, data[key][i]));
                });
        });

    // method two

    const otherMethod = {
        [group]: Object.assign(
            {},
            ...dataKeys.map((key) =&gt; {
                return {
                    [key] : count
                        .map((n, i) =&gt; dataKeys
                            .map(key =&gt; arrayLengthN(n, data[key][i]))
                        )
                        .flat(2)
                };
            })
        )};

    return {[group]: output}
    // or return otherMethod
}


Is there anything wrong with the first method? It feels clunky to create an object, add empty arrays to it, and then mutate that object from within a .map call. That being said, to me it seems far more readable.

Any help or comments much appreciated.

Cheers,

P
May i suggest a shorter code?



var data = {
  dataGroup: {
    count: [4, 4, 2, 1],
      dataOne: [1, 3, 2, 1],
      dataTwo: [7, -1, 9, 0],
      dataThree: [3, 8, 1, 2]
  }
}

var processedData = Object.keys(data.dataGroup).filter(key =&gt; key != 'count').reduce((acc, key) =&gt; {
  acc.groupName[key] = [];
  data.dataGroup.count.forEach((amount, index) =&gt; {
    for (let i = 0; i &lt; amount; i++) {
      acc.groupName[key].push(data.dataGroup[key][index]);
    }
  });
  return acc;
}, { groupName: {}});
console.log(processedData);