1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 17const array1 = [5, 12, 8, 130, 44]; 18const found = array1.find((element) => element > 10); 19console.log(found); 20 21const arrayLike = { 22 length: 3, 23 0: 2, 24 1: 7.3, 25 2: 4, 26}; 27console.log(Array.prototype.find.call(arrayLike, (x) => !Number.isInteger(x))); 28 29const array = [0, 1, , , , 5, 6]; 30 31array.find((value, index) => { 32 console.log(`${index},${value}`); 33}); 34 35array.find((value, index) => { 36 if (index === 0) { 37 console.log(`array[5]${array[5]}`); 38 delete array[5]; 39 } 40 console.log(`${index},${value}`); 41}); 42 43function isPrime(element, index, array) { 44 let start = 2; 45 while (start <= Math.sqrt(element)) { 46 if (element % start++ < 1) { 47 return false; 48 } 49 } 50 return element > 1; 51} 52 53console.log([4, 6, 8, 12].find(isPrime)); 54console.log([4, 5, 8, 12].find(isPrime)); 55// 56const numbers = [3, -1, 1, 4, 1, 5, 9, 2, 6]; 57const firstTrough = numbers 58 .filter((num) => num > 0) 59 .find((num, idx, arr) => { 60 if (idx > 0 && num >= arr[idx - 1]) return false; 61 if (idx < arr.length - 1 && num >= arr[idx + 1]) return false; 62 return true; 63 }); 64console.log(firstTrough); // 1 65 66const words = ["spray", "limit", "limits"]; 67const deleteWords = words.find((word, index, arr) => { 68 arr.length=4 69 word="asd" 70 return word == "asd" 71}); 72console.log(deleteWords); 73console.log(words.length); 74 75