0

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, that can then be passed into subsequent functions (to render graph data)

I have an array of data:

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,],
                        ];

I also setup an array called switch

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

In my code, I loop through the length of the switch array, and I want to remove the corresponding column or index of each line in the array dataArray

function workingDataArray(){
    workingArray = null;
    workingArray = dataArray.slice();
    var switchLength = switch.length;
    for (var i = 0; i < switchLength; i++) {
        if(!switch[i]){
            //Remove every item in the position if the switch is true
        }
    }
    return workingArray;
}

The idea here, is that if I change switch[3] and switch[5] to true, it will return:

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

I'm not even sure if this is the best way to go about this, but it kind of makes sense to me, but I think I need some help getting in the right direction.

mrpatg
  • 9,393
  • 40
  • 101
  • 161

4 Answers4

1
Array.prototype.applyReduceModel = function( model ){
    var all = [];
    this.forEach( function( row ){
        if( row instanceof Array ){
            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 );
karkael
  • 350
  • 1
  • 7
1

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],
];

function removeColumns(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 adjust
        var indexAdjustment = 0;
        // copy row w/ .slice so we do not modify the original array
        var _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 remove
var 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 unchanged
console.log(dataArray);
mhodges
  • 9,573
  • 1
  • 22
  • 43
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],
];


function reduceMyArray(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.

Matt
  • 989
  • 1
  • 7
  • 11
0
  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;


function UpdateAndReturnNewArray(){
    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());
Abhinav Galodha
  • 7,820
  • 2
  • 27
  • 37
  • `.map(x=>x)` is equivalent to `.slice()`. Also, since you are splicing the nested arrays, neither `.map(x=>x)`, nor `.slice()` will do a deep copy, so any modifications to any nested arrays *are* actually modifying the original. If you `console.log(dataArray)` after you call your update function, you will see that `dataArray` is changed. – mhodges Jan 03 '18 at 18:57
  • Your posted solution also does not remove any data elements except from the first row.. `newArr[0]` only affects the first row. – mhodges Jan 03 '18 at 18:59