0

I have the following array

var array = [[0],[0],[0],["noteremove",1],[10],["notremove",1]]

Which i want to end looking like:

[["noteremove",1],[10],["notremove",1]]

Meaning that all [0] should be purged.

I have this, but it doesnt work:

        function arraysEqual(a1,a2) {
        return JSON.stringify(a1)==JSON.stringify(a2);
    }

    var array = [[0],[0],[0],["noteremove",1],[10],["notremove",1]]

    for (let i = 0; i< array.length;i++){
        if(arraysEqual(array[i],[0])){ 
            console.log("its equal")  
            array.splice(i,1)
        } 
    }

    console.log(array); //It logs 
//[ [ 0 ], [ 'noteremove', 1 ], [ 10 ], [ 'notremove', 1 ] ] 
//The initial 0 shouldnt be there
mouchin777
  • 1,428
  • 1
  • 31
  • 59
  • 1
    Consider using `filter` instead? – evolutionxbox Mar 21 '20 at 13:03
  • after you splice the first element, the second element becomes the first and your index becomes 1, skipping a [0] element. Always be careful removing elements while iterating using an index – Phu Ngo Mar 21 '20 at 13:04

4 Answers4

2

You could use filter method and check if length is 1 and first element is 0.

var array = [[0],[0],[0],["noteremove",1],[10],["notremove",1]]
var result = array.filter(e => !(e.length == 1 && e[0] === 0))
console.log(result)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
1

A simple loop should get the work done:

let array = [[0],[0],[0],["noteremove",1],[10],["notremove",1]]
let new_array = []
array.forEach(function(element){
    if(JSON.stringify(element)!=JSON.stringify([0])){
        new_array.push(element)
    }
})
console.log(new_array)

I'm using here stringify to compare both arrays, although, there are other methods

Gamopo
  • 1,600
  • 1
  • 14
  • 22
1

You can try this-

var array = [[0],[0],[0],["noteremove",1],[10],["notremove",1]]
const result = [];

array.forEach(v => {
  if (v.length === 1 && v[0] === 0) {
    // Do nothing
  } else {
    result.push(v)
  }
});

console.log(result);
Sajeeb Ahamed
  • 6,070
  • 2
  • 21
  • 30
1

Using .splice() within your an incrementing for loop will shift the indexes of all values once a value is removed, causing you to miss some values. If you loop through the array backwards then removing elements won't shift the values to lower indexes, allowing you to remove all matches:

function arraysEqual(a1, a2) {
  return JSON.stringify(a1) === JSON.stringify(a2);
}

var array = [[0],[0],[0],["noteremove",1],[10],["notremove",1]];

for (let i = array.length-1; i >= 0; i--) {
  if (arraysEqual(array[i], [0])) {
    array.splice(i, 1)
  }
}
console.log(array);

Instead, an easier approach would be to use .filter(), where you can provide a callback function which gets call on each element in your array. If the callback returns true the item is kept, if it returns false the item is removed from the new array:

const array = [[0],[0],[0],["noteremove",1],[10],["notremove",1]];
const res = array.filter(arr => arr.length !== 1 || arr[0] !== 0);
console.log(res);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64