js map與forEach 資料遍歷處理
阿新 • • 發佈:2021-12-02
- find替換查詢符合條件資料:返回符合條件的第一個資料
// find 返回第一個合適的就停止 // 在函式中如果找到符合條件的陣列元素就進行return,並停止查詢。 // 你可以拷貝下邊的程式碼進行測試,就會知道find作用。 let a = data.find(item => item.age > 20) console.log(a); // forEach 無返回值:所以輸出undefined // map 有返回值:符合條件輸出item,不符合預設return;,輸出undefined let aa = data.map(item => { iflet bb = data.forEach(item => { if(item.age > 20) { return item; } }) let cc = data.filter(item => { if(item.age > 20) { return item; } }) let has = data.some(item => item.age > 30 ) let has2 = data.every(item => item.age > 10)(item.age > 20) { return item; } })