Remove arrays from an Array if a certain values exists within a list

Pretty new at JavaScript, just trying to remove an entire array within my data if certain values are listed in the del_values array.

What would be the best approach to this problem?

data = 
[['true', 'visiting-today', 'DVM-Wiessman','J-001'],
['false', 'visiting-tommorrow', 'DVM-Stevens','K-001'],
['true', 'visiting-tommorrow', 'DVM-Stevens','Z-001'],
['false', 'visiting-tommorrow', 'DVM-Kon','J-001']]


var del_values = ['J-001','K-001'];

function remove_from_list(list,deleted_values) {
    for( var i = 0; i < deleted_values.length; i++) { 
        result = list.filter(dat => !dat.includes(deleted_values[i]));
    }
    return result;
}

Actual Result

data = 
[ [ 'true', 'visiting-today', 'DVM-Wiessman', 'J-001' ],
  [ 'true', 'visiting-tommorrow', 'DVM-Stevens', 'Z-001' ],
  [ 'false', 'visiting-tommorrow', 'DVM-Kon', 'J-001' ] ]

Desired Result -- to remove an entire array within the data if it contains any of the values listed in del_values

data = 
[[ 'true', 'visiting-tommorrow', 'DVM-Stevens', 'Z-001' ] ]


Solution 1:

You can use Array.prototype.every inside filter's callback:

const data = [
  ['true', 'visiting-today', 'DVM-Wiessman', 'J-001'],
  ['false', 'visiting-tommorrow', 'DVM-Stevens', 'K-001'],
  ['true', 'visiting-tommorrow', 'DVM-Stevens', 'Z-001'],
  ['false', 'visiting-tommorrow', 'DVM-Kon', 'J-001']
]

const del_values = ['J-001', 'K-001'];

function remove_from_list(list, deleted_values) {
  return list.filter(arr => arr.every(el => !deleted_values.includes(el)))
}

console.log(remove_from_list(data, del_values))

function remove_from_list(list,deleted_values) {
    for( var i = 0; i < deleted_values.length; i++) { 
        result = list.filter(dat => !dat.includes(deleted_value[i]));
    }
    return result;
}

The main issue in your code is at each iteration list is being filtered so the previously filtered array gets reassigned.

Solution 2:

Using Array.some() and Array.filter()

var data = 
[['true', 'visiting-today', 'DVM-Wiessman','J-001'],
['false', 'visiting-tommorrow', 'DVM-Stevens','K-001'],
['true', 'visiting-tommorrow', 'DVM-Stevens','Z-001'],
['false', 'visiting-tommorrow', 'DVM-Kon','J-001']]


var del_values = ['J-001','K-001'];

var result = data.filter(function(arr){
  return !arr.some(function(e){return del_values.includes(e)});
})

console.log(result)

Solution 3:

You're running your filter through list instead of result each time.

I think you intended to continue working on the filtered list rather than the argument all over again with each iteration:

    data = 
    [['true', 'visiting-today', 'DVM-Wiessman','J-001'],
    ['false', 'visiting-tommorrow', 'DVM-Stevens','K-001'],
    ['true', 'visiting-tommorrow', 'DVM-Stevens','Z-001'],
    ['false', 'visiting-tommorrow', 'DVM-Kon','J-001']]
    
    
    var del_values = ['J-001','K-001'];
    
    function remove_from_list(list,deleted_values) {
        for( var i = 0; i < deleted_values.length; i++) { 
            list = list.filter(dat => !dat.includes(deleted_values[i]));
        }
        return list;
    }
    
    let e = remove_from_list(data,del_values);
    console.log(e);