Remove an array from an Array if a certain value exists in the array in javascript
Solution 1:
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 input = "J-001";
// Approach
// Filter element (use filter) who has not given input (use not includes for this)
const result = data.filter(dat => !dat.includes(input));
console.log(result);
filter includes
Solution 2:
You can use array#filter
and array#includes
to filter based on array not including J-001
.
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']],
result = data.filter(arr => !arr.includes('J-001'));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
If the position of the element is fixed to be third. Then you can use directly compare the value at that index.
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']],
result = data.filter(arr => arr[3] !== 'J-001');
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }