0

I'm totally new to Javascript. I'm working with data from a business database and I want to remove an array from array of arrays.

I have this array of array:

var results = [ [#null, null], ['miglioramento', 30], ['correttiva',45] ];

I want to remove the first array: [#null, null] from results because I want to achieve this array:

results = [ ['miglioramento', 30], ['correttiva',45] ];

How can I do this?

I really thank you in advance!

Nick Parsons
  • 31,322
  • 6
  • 25
  • 44

5 Answers5

1

Assuming the first entry of your array is [null, null] not [#null, null]. This way you are filtering all empty arrays containing only null as entries.

results.filter((arr) => arr.filter((entry) => entry !== null).length);

chr
  • 393
  • 3
  • 7
0

You can use slice function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

const array = [0, 1, 2, 3]
const slicedArray = array.slice(1)
console.log(slicedArray)
// print : [1, 2, 3]
Louis Singer
  • 488
  • 1
  • 6
  • 15
0

For inner arrays with only two elements, you can use .filter() and destructure the elements to check whether one of the elements isn't equal to null. If this is the case, your filter predicate will return true, which will keep the inner array.

const data = [[null, null], ['miglioramento', 30], ['correttiva',45]];

const res = data.filter(([e1, e2]) => e1 !== null || e2 !== null);
console.log(res);

If your inner arrays can have more than two elements, you can use .some() to ensure that some element within the inner array is not null. If the predicate for .some() is true for any element within your inner array, it the return value of .some() will also be true which will make filter keep the inner array as part of the end result.

const data = [[null, null], ['miglioramento', 30], ['correttiva',45]];

const res = data.filter(
  inner => inner.some(elem => elem !== null)
);
console.log(res);
Nick Parsons
  • 31,322
  • 6
  • 25
  • 44
0

create a function to check if an arry is all null then call it inside a filter function.

var results = [
  [null, null],
  ['miglioramento', 30],
  ['correttiva', 45]
];


const checkNull = arr => arr.every(el => el != null);

const res = results.filter(el => checkNull(el));

console.log(res);
G.aziz
  • 3,101
  • 1
  • 11
  • 28
-2

var results = [ [null, null], ['miglioramento', 30], ['correttiva',45] ]
results.shift()
console.log(results)