#每天一个知识点#
如果有个arr( 数组)很大,可以考虑使用 filter 方法来过滤不需要的元素,而不是使用 forEach 遍历每个元素并检查。这可以减少遍历次数,提高效率 。
let diwnList = arr.filter(item => item.name !== '条件');
如果arr数组中的元素很多,而且它们的 name属性值经常变化,可以考虑使用 Set 数据结构来存储这些值,并使用 has 方法来检查一个值是否存在于这个集合中。这可以避免每次比较字符串的代价,提高性能。
let excludeTables = new Set(['条件']);
let diwnList = [];
arr.forEach(item => {
if (!excludeTables.has(item.name)) {
diwnList.push(item);
}
});
具体哪种更适合取决于具体的应用场景和数据特点。
如果有个arr( 数组)很大,可以考虑使用 filter 方法来过滤不需要的元素,而不是使用 forEach 遍历每个元素并检查。这可以减少遍历次数,提高效率 。
let diwnList = arr.filter(item => item.name !== '条件');
如果arr数组中的元素很多,而且它们的 name属性值经常变化,可以考虑使用 Set 数据结构来存储这些值,并使用 has 方法来检查一个值是否存在于这个集合中。这可以避免每次比较字符串的代价,提高性能。
let excludeTables = new Set(['条件']);
let diwnList = [];
arr.forEach(item => {
if (!excludeTables.has(item.name)) {
diwnList.push(item);
}
});
具体哪种更适合取决于具体的应用场景和数据特点。
展开
1
2