gpt4 book ai didi

javascript - 如果javascript中的数组中存在某个值,则从数组中删除数组

转载 作者:行者123 更新时间:2023-12-05 09:29:18 24 4
gpt4 key购买 nike

如果在我的数据中包含特定值,则尝试删除整个数组。

解决这个问题的最佳方法是什么?

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

实际输出

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

期望的结果——删除所有值为 'J-001'

的数组
data = 
[['false', 'visiting-tommorrow', 'DVM-Stevens','K-001'],
['true', 'visiting-tommorrow', 'DVM-Stevens','Z-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']];

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

关于javascript - 如果javascript中的数组中存在某个值,则从数组中删除数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70750373/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com