0

Is there a clever way of substituting / replacing a value in an array

eg.

var array = [[1,2,3,'null'],['null',2,3,4],['null','null','null','null'],[1,1,1,1]]

the answer should be which i will write to a spreadsheet

[[1,2,3,''],['',2,3,4],['','','',''],[1,1,1,1]]

The actual array is much bigger than this and spans 1000+ lines

Rubén
  • 34,714
  • 9
  • 70
  • 166
mfaiz
  • 475
  • 1
  • 7
  • 17
  • @Max Makhrov Thanks your answer seems to work best. As quite rightly pointed out google spreadsheet scripts cannot accept =>. I would liked ot have used the elegance of map but cannot. – mfaiz Nov 30 '17 at 15:10

5 Answers5

3

If you can use Array.prototype.map, you could do something like:

var arrayWithoutNulls = array.map(line => line.map(el => el === 'null' ? '' : el))

Edit as per @charlietfl suggestion: this returns a new array. So bind it to a new variable (as the example above) or immediately return it, but bear in mind the array variable will still have the null elements.

Matheus208
  • 1,289
  • 2
  • 13
  • 26
1

If you want to modify the original array:

var arrs = [
  [1, 2, 3, 'null'],
  ['null', 2, 3, 4],
  ['null', 'null', 'null', 'null'],
  [1, 1, 1, 1]
];

arrs.forEach(function(arr, i) {
  arrs[i] = arr.join(",").replace(/null/g, '').split(",");
})

console.log(arrs);
1

Please try:

var array = [[1,2,3,'null'],['null',2,3,4],['null','null','null','null'],[1,1,1,1]];


var row = [];
var cols = array[0].length;

for (var i = 0, l = array.length; i < l; i++)
{
  row = array[i];
  for (var col = 0; col < cols; col++)
  {
    if (row[col] === 'null') { row[col] = ''; } 
  }
}

Logger.log(array); // [[1, 2, 3, ], [, 2, 3, 4], [, , , ], [1, 1, 1, 1]]

Note: arrows => do not work in Google Sheets, used a simple loop for a better performance.

Max Makhrov
  • 17,309
  • 5
  • 55
  • 81
  • Thanks your answer seems to work best. As quite rightly pointed out google spreadsheet scripts cannot accept =>. I would liked to have used the elegance of map but cannot. @Peter Leger for whatever reason this method seems to throw out an error when writing the array to spreadsheet, somewhere it has delete an element from an array which says that the array is of incorrect width. – mfaiz Nov 30 '17 at 15:12
  • @mfizz: `@replies` only fire a notification when the mentioned user participate on the post, either as author, editor or commentator, son Peter Leger perhaps doesn't know about your comment. – Rubén Nov 30 '17 at 19:22
0

You'll need to use map. First on the containing array, then on the ones nested inside:

var array = [
  [1, 2, 3, 'null'],
  ['null', 2, 3, 4],
  ['null', 'null', 'null', 'null'],
  [1, 1, 1, 1]
]

const newArray = array.map(arr => {
  return arr.map(elem => {
    return elem === 'null' ? '' : elem;
  });
});

console.log(newArray);
Carl Edwards
  • 13,826
  • 11
  • 57
  • 119
0

To modify orig array you could actually use a normal foreach loop and then replace items in array with the splice method

var array = [[1,2,3,'null'],['null',2,3,4],['null','null','null','null'],[1,1,1,1]]


array.forEach(a => a.forEach((b,i) => {
  if (b === 'null') {
    a.splice(i, 1 , '')
  }
}))
console.log(array)
FrankCamara
  • 348
  • 4
  • 9