Skip to content Skip to sidebar Skip to footer

Removing Columns Of Data In Javascript Array

I have a generated set of data that I've formatted into an array. I need to preserve the initial set of data, but generate a modified array as output in the form of a function, tha

Solution 1:

You can do this with .map and .filter by doing the following:

var switcher = [true, true, false, false, false, true, true],

dataArray = [
    ['Day', '1', '2', '3', '4', '5', '6'],
    ['Day -7',0,0,0,0,0,0],
    ['Day -6',0,0,0,0,0,0],
    ['Day -5',0,0,0,0,0,0],
    ['Day -4',0,0,0,0,0,0],
    ['Day -3',0,0,0,0,0,0],
    ['Day -2',0,0,0,0,0,0],
];


functionreduceMyArray(arr){
    return arr.map(function(x, index){
        return x.filter(function(y, index1){
            return switcher[index1] === true;
        });
    });
}

var x = reduceMyArray(dataArray);

.map will return your array when everything is complete while the filter goes through each row checking the value of switcher and return the values where the index in switcher is true.

Solution 2:

Array.prototype.applyReduceModel = function( model ){
    var all = [];
    this.forEach( function( row ){
        if( row instanceofArray ){
            var res = [];
            row.forEach( function( el, k ){
                if( !model[ k ] )
                    res.push( el );
            });
            all.push( res );
        }
    });
    return all;
}

var switcher = [true, true, true, false, true, false, true],
    dataArray = [
        ['Day', '1', '2', '3', '4', '5', '6'],
        ['Day -7',0,0,0,0,0,0,],
        ['Day -6',0,0,0,0,0,0,],
        ['Day -5',0,0,0,0,0,0,],
        ['Day -4',0,0,0,0,0,0,],
        ['Day -3',0,0,0,0,0,0,],
        ['Day -2',0,0,0,0,0,0,],
    ];

dataArray.applyReduceModel( switcher );

Solution 3:

You can do it like this. .slice() will be your friend, since you do not want to modify the original array. Other than that, it's pretty straightforward. Get an array of indexes you want to remove, adjust for splice()ing, and return the modified copy of the array.

var dataArray = [
    ['Day', '1', '2', '3', '4', '5', '6'],
    ['Day -7',0,0,1,0,0,0],
    ['Day -6',0,0,1,0,0,0],
    ['Day -5',0,0,1,0,0,0],
    ['Day -4',0,0,1,0,0,0],
    ['Day -3',0,0,1,0,0,0],
    ['Day -2',0,0,1,0,0,0],
];

functionremoveColumns(data, indexes) {
    return data.map(function (row) {
        // when we remove columns, the indexing gets off by 1 each time, keep track of how many to adjustvar indexAdjustment = 0;
        // copy row w/ .slice so we do not modify the original arrayvar _row = row.slice();
        indexes.forEach(function (colIndex) {
            // remove column
            _row.splice(colIndex - indexAdjustment, 1);
            // add 1 to adjustment to account for the column we just removed
            indexAdjustment++
        });
        return _row;
    });
}  

var switches = [false, false, false, true, false, true, false];
// get array of indexes to removevar indexesToRemove = switches.reduce(function (indexes, swtch, index) {
  if (swtch) {
    indexes.push(index);
  }
  return indexes;
}, []);
console.log("---- MODIFIED ARRAY ----");
console.log(removeColumns(dataArray, indexesToRemove));
console.log("\n---- ORIGINAL ARRAY ----");
// original is left unchangedconsole.log(dataArray);

Solution 4:

  1. switch is a reserved keyword.So, you need to rename the variable switch.
  2. You can make use of the splice to remove the elements of the Array and also make use of map to transform an array into another array.

Following is the working snippet

dataArray = [
['Day', '1', '2', '3', '4', '5', '6'],
['Day -7',0,0,0,0,0,0,],
['Day -6',0,0,0,0,0,0,],
['Day -5',0,0,0,0,0,0,],
['Day -4',0,0,0,0,0,0,],
['Day -3',0,0,0,0,0,0,],
['Day -2',0,0,0,0,0,0,],
                        ];

myswitch = [];
myswitch[0] = false;
myswitch[1] = false;
myswitch[2] = false;
myswitch[3] = true;
myswitch[4] = false;
myswitch[5] = true;
myswitch[6] = false;


functionUpdateAndReturnNewArray(){
    var newArr = dataArray.map(x=>x);
    var j = 0;
    var switchLength = myswitch.length;
    for (var i = 0; i < switchLength; i++) {
       
        if(myswitch[i]){
           newArr[0].splice(j,1);
           j = i - 1 ;
         }
          j++;
    }
    return newArr;
}

console.log(UpdateAndReturnNewArray());

Post a Comment for "Removing Columns Of Data In Javascript Array"