Compress repetition of 2 dimensional array with javascript

Viewed 180
var mycars = [['Ford', 'Fiesta', 'Manual', 'Rp. 160.000.000'],
            ['Ford', 'Fiesta', 'Manual', 'Rp. 163.000.000'],
            ['Ford', 'Fiesta', 'Automatic', 'Rp. 170.000.000'],
            ['Ford', 'Fiesta', 'Automatic', 'Rp. 175.000.000'],
            ['Ford', 'Fiesta', 'Automatic', 'Rp. 173.000.000'],
            ['Ford', 'Focus', 'Manual', 'Rp. 350.000.000'],
            ['Ford', 'Focus', 'Automatic', 'Rp. 400.000.000'],
            ['VW', 'Golf', 'Manual', 'Rp. 360.000.000'],
            ['VW', 'Golf', 'Manual', 'Rp. 365.000.000'],
            ['VW', 'Golf', 'Automatic', 'Rp. 390.000.000']];

Above is 2 dimensional array that has a lot of data repetition, that array need to simplify with provision as below:

  • index data 0, must be full (no changes)
  • data after index 0, if the value is the same as the previous index, change it to ''
  • result data must remain in 2-dimensional array form

So the result will be as below

  • index data 0, must be full (no changes)
  • data after index 0, if the value is the same as the previous index, change it to ''
  • result data must remain in 2-dimensional array form

So the result will show as below:

var mycars_compressed = [['Ford', 'Fiesta', 'Manual', 'Rp. 160.000.000'],
            ['', '', '', 'Rp. 163.000.000'],
            ['', '', 'Automatic', 'Rp. 170.000.000'],
            ...
            ['', '', 'Automatic', 'Rp. 400.000.000'],
            ['VW', 'Golf', 'Manual', 'Rp. 360.000.000'],
            ['', '', '', 'Rp. 365.000.000'],
            ...
];

And the result above could be decompressed again like the beginning.

2 Answers

You could take a temporary array and check the value against the value of the tmeporary array at the same index and if the same take an empty string '', or take the value by storing that value to the temporary array.

For decompressing, you could take an assigment to the temporary array and check if the actual value is truthy, then take that value or take the value of the temporary array. Return that value.

var data = [['Ford', 'Fiesta', 'Manual', 'Rp. 160.000.000'], ['Ford', 'Fiesta', 'Manual', 'Rp. 163.000.000'], ['Ford', 'Fiesta', 'Automatic', 'Rp. 170.000.000'], ['Ford', 'Fiesta', 'Automatic', 'Rp. 175.000.000'], ['Ford', 'Fiesta', 'Automatic', 'Rp. 173.000.000'], ['Ford', 'Focus', 'Manual', 'Rp. 350.000.000'], ['Ford', 'Focus', 'Automatic', 'Rp. 400.000.000'], ['VW', 'Golf', 'Manual', 'Rp. 360.000.000'], ['VW', 'Golf', 'Manual', 'Rp. 365.000.000'], ['VW', 'Golf', 'Automatic', 'Rp. 390.000.000']],
    compressed = data.map((t => a => a.map((v, i) => v === t[i]
        ? ''
        : t[i] = v
    ))([])),
    decompressed =data.map((t => a => a.map((v, i) => t[i] = v || t[i]))([]));

console.log(compressed);
console.log(decompressed);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Its a data structure question.

You gonna need 2 for loops. Logic is simple will be using 2 more array one 2D and one 1D:

  1. Assign first row of arr1 to arr2.
  2. Leave first row and start loop with second one of arr1 i=1.
  3. Set arr3 to empty array.
  4. Start another loop for inner array j=0.
  5. Check if arr1[i][j] === arr[i-1][j] Yes then push '' to arr3.push(''), if Not then push arr1[i][j] to arr3.push(arr1[i][j]).
  6. Complete 2nd loop.
  7. Assign arr3 to arr2.push(arr3).
  8. Complete outer loop.

Here's working logic

`

var a1 = [], a2 = [];
let m = mycars.length
    n = mycars[0].length;

a1.push(mycars[0]);

for(let i = 1; i<m; i++) {
   a2 = [];

   for(let j =0; j<n; j++){

       if(mycars[i][j] == mycars[i-1][j]) {
           a2.push('');
       } else {
           a2.push(mycars[i][j]);
       }
   }
   //This is compressed array
   a1.push(a2);
}

var mycars_compressed = a1; 

`

Related