最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript中filter() 和find()的區(qū)別對(duì)比小結(jié)

 更新時(shí)間:2025年05月08日 10:28:23   作者:胡歌1  
filter()?和?find()?都是 JavaScript 數(shù)組的高階函數(shù),用于搜索數(shù)組元素,本文主要介紹了JavaScript中filter() 和find()的區(qū)別對(duì)比,感興趣的可以了解一下

filter() 和 find() 都是 JavaScript 數(shù)組的高階函數(shù),用于搜索數(shù)組元素,但它們有幾個(gè)關(guān)鍵區(qū)別:

1. 基本區(qū)別

特性filter()find()
返回值新數(shù)組(包含所有匹配元素)第一個(gè)匹配的元素(不是數(shù)組)
空結(jié)果返回空數(shù)組 []返回 undefined
用途篩選多個(gè)符合條件的元素查找第一個(gè)符合條件的元素

2. 代碼示例對(duì)比

示例數(shù)組

const users = [
  { id: 1, name: 'Alice', active: true },
  { id: 2, name: 'Bob', active: false },
  { id: 3, name: 'Charlie', active: true },
  { id: 4, name: 'David', active: true }
];

使用 filter()

// 找出所有活躍用戶
const activeUsers = users.filter(user => user.active);
console.log(activeUsers);
// 輸出: [
//   {id: 1, name: 'Alice', active: true},
//   {id: 3, name: 'Charlie', active: true},
//   {id: 4, name: 'David', active: true}
// ]

// 沒有匹配項(xiàng)時(shí)
const noUsers = users.filter(user => user.age > 100);
console.log(noUsers); // 輸出: []

使用 find()

// 查找第一個(gè)活躍用戶
const firstActive = users.find(user => user.active);
console.log(firstActive);
// 輸出: {id: 1, name: 'Alice', active: true}

// 沒有匹配項(xiàng)時(shí)
const notFound = users.find(user => user.age > 100);
console.log(notFound); // 輸出: undefined

3. 性能考慮

  • filter() 會(huì)遍歷整個(gè)數(shù)組,即使已經(jīng)找到所有符合條件的元素

  • find() 在找到第一個(gè)匹配項(xiàng)后立即停止遍歷

// 性能測(cè)試
const bigArray = Array(1000000).fill(0).map((_, i) => i);

console.time('filter');
bigArray.filter(x => x === 500000); // 會(huì)檢查所有元素
console.timeEnd('filter'); // 輸出: filter: 5.22509765625 ms

console.time('find');
bigArray.find(x => x === 500000); // 找到后立即停止
console.timeEnd('find'); // 輸出: find: 2.56298828125 ms

4. 鏈?zhǔn)秸{(diào)用差異

// filter可以繼續(xù)鏈?zhǔn)秸{(diào)用
users
  .filter(user => user.active)
  .map(user => user.name)
  .forEach(name => console.log(name));
// 輸出: 
// Alice
// Charlie
// David

// find不能鏈?zhǔn)秸{(diào)用數(shù)組方法(因?yàn)榉祷氐氖窃兀?
const userName = users.find(user => user.id === 2).name; // 直接訪問屬性
console.log(userName) //輸出: Bob

5. 實(shí)際應(yīng)用場(chǎng)景

適合使用 filter() 的情況:

  • 需要獲取所有匹配項(xiàng)

  • 需要對(duì)結(jié)果集進(jìn)行進(jìn)一步操作(如再過濾、映射等)

  • 需要確??偸欠祷?cái)?shù)組(便于后續(xù)處理)

// 獲取所有未完成的任務(wù)
const incompleteTasks = tasks.filter(task => !task.completed);

// 結(jié)合map使用
const activeUserNames = users
  .filter(user => user.active)
  .map(user => user.name);

適合使用 find() 的情況:

  • 只需要第一個(gè)匹配項(xiàng)

  • 檢查數(shù)組中是否存在某個(gè)元素

  • 查找特定ID的對(duì)象

// 查找特定用戶
const user = users.find(user => user.id === 3);
console.log(user) //輸出: { id: 3, name: 'Charlie', active: true },

// 檢查是否存在管理員
const hasAdmin = users.find(user => user.role === 'admin') !== undefined;
console.log(hasAdmin ) //輸出: false

6. 特殊注意事項(xiàng)

引用類型:兩者都返回原始數(shù)組中的引用(不會(huì)創(chuàng)建副本)

const found = users.find(u => u.id === 1);
found.name = 'Alex'; // 會(huì)修改原數(shù)組中的對(duì)象
console.log(users) 
//輸出: [
//  { id: 1, name: 'Alex', active: true },
//  { id: 2, name: 'Bob', active: false },
//  { id: 3, name: 'Charlie', active: true },
//  { id: 4, name: 'David', active: true }
// ]

稀疏數(shù)組

const sparse = [1, , 3];
sparse.find(x => true); // 1 (跳過空位)
sparse.filter(x => true); // [1, 3] (跳過空位)

this綁定:兩者都接受第二個(gè)參數(shù)用于設(shè)置回調(diào)函數(shù)的this

const checker = {
  threshold: 2,
  check(num) { return num > this.threshold; }
};

[1, 2, 3].find(checker.check, checker); // 3

總結(jié)選擇

  • 需要 多個(gè)結(jié)果 → 用 filter()

  • 只需要 第一個(gè)結(jié)果 → 用 find()

  • 需要 性能優(yōu)化(大數(shù)據(jù)集) → 優(yōu)先考慮 find()

  • 需要 確保數(shù)組返回值 → 用 filter()

  • 進(jìn)行 鏈?zhǔn)讲僮?/strong> → 用 filter()

到此這篇關(guān)于JavaScript中filter() 和find()的區(qū)別對(duì)比小結(jié)的文章就介紹到這了,更多相關(guān)JavaScript filter() 和find()內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

宁武县| 景德镇市| 响水县| 全南县| 寻甸| 手机| 乌拉特前旗| 台东市| 巩义市| 新昌县| 含山县| 肥乡县| 拉孜县| 汉寿县| 临江市| 东辽县| 准格尔旗| 繁峙县| 沙雅县| 深圳市| 梓潼县| 宣恩县| 乐安县| 武功县| 南澳县| 罗城| 湘潭县| 肃宁县| 旌德县| 岐山县| 东乡县| 晋宁县| 无锡市| 上饶县| 乐业县| 油尖旺区| 福建省| 凤凰县| 新田县| 区。| 金门县|