2

Trying to remove an entire array if within my data if it contains a certain value.

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_value = 'J-001';
function remove_from_list(list) {
 for( var i = 0; i < list.length; i++) { 
 for( var j =0; j < Object.keys(list[i]).length.length; j++) {
 if(del_value == list[i][j]) {
 list.splice(list[i], 1);
 }
 }
 }
 return list;
}

actual output

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']]

desired result -- to remove all arrays that have the value 'J-001'

data = 
[['false', 'visiting-tommorrow', 'DVM-Stevens','K-001'],
['true', 'visiting-tommorrow', 'DVM-Stevens','Z-001']]
asked Jan 18, 2022 at 4:43
1
  • @HassanImam yeah, it would be Commented Jan 18, 2022 at 4:48

3 Answers 3

3

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

answered Jan 18, 2022 at 4:52
Sign up to request clarification or add additional context in comments.

Comments

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; }

answered Jan 18, 2022 at 4:48

Comments

2

Alternate approach using Array.filter() and Array.find().

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']];
function removeFromList(list, delValue) {
 return list.filter(arr => !arr.find(value => value === delValue));
}
console.log(removeFromList(data, 'J-001'));

answered Jan 18, 2022 at 5:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.