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); 19print(found); 20 21const arrayLike = { 22 length: 3, 23 0: 2, 24 1: 7.3, 25 2: 4, 26}; 27print(Array.prototype.find.call(arrayLike, (x) => !Number.isInteger(x))); 28 29const array = [0, 1, , , , 5, 6]; 30 31array.find((value, index) => { 32 print(`${index},${value}`); 33}); 34 35array.find((value, index) => { 36 if (index === 0) { 37 print(`array[5]${array[5]}`); 38 delete array[5]; 39 } 40 print(`${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 53print([4, 6, 8, 12].find(isPrime)); 54print([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 }); 64print(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}); 72print(deleteWords); 73print(words.length); 74 75const findTestCases = [ 76 () => { 77 let log = []; 78 const result = [1, 2, 3, 4].find((x, i, arr) => { 79 log.push({value: x, index: i, array: [...arr]}); 80 return x === 3; 81 }); 82 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 3 83 }, 84 85 () => { 86 let log = []; 87 const result = [1, 2, 3, 4].find((x, i, arr) => { 88 log.push({value: x, index: i, array: [...arr]}); 89 return x === 5; 90 }); 91 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: undefined 92 }, 93 94 () => { 95 let log = []; 96 const result = [].find((x, i, arr) => { 97 log.push({value: x, index: i, array: [...arr]}); 98 return x > 0; 99 }); 100 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: undefined 101 }, 102 103 () => { 104 let log = []; 105 const result = [false, 0, ''].find((x, i, arr) => { 106 log.push({value: x, index: i, array: [...arr]}); 107 return !x; // 找到 falsy 值 108 }); 109 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: false 110 }, 111 112 () => { 113 let log = []; 114 const result = [1, 2, 3, 4].find((x, i, arr) => { 115 log.push({value: x, index: i, array: [...arr]}); 116 return x > 2; // 第一个大于 2 的元素 117 }); 118 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 3 119 }, 120 121 () => { 122 let log = []; 123 const result = [null, 1, 2].find((x, i, arr) => { 124 log.push({value: x, index: i, array: [...arr]}); 125 return x !== null && x !== undefined; 126 }); 127 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 1 128 }, 129 130 () => { 131 let log = []; 132 let arr = [1, 2, 3]; 133 const result = arr.find((x, i, arrRef) => { 134 log.push({value: x, index: i, array: [...arrRef]}); 135 if (i === 1) arrRef.push(4); // 修改数组 136 return x === 3; 137 }); 138 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 3 139 }, 140 141 () => { 142 let log = []; 143 const result = [NaN, 1, 2].find((x, i, arr) => { 144 log.push({value: x, index: i, array: [...arr]}); 145 return !isNaN(x); 146 }); 147 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 1 148 }, 149 150 () => { 151 let log = []; 152 const result = [1, 2, 3].find((x, i, arr) => { 153 log.push({value: x, index: i, array: [...arr]}); 154 return false; 155 }); 156 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: undefined 157 }, 158 159 () => { 160 let log = []; 161 const result = [1, 2, 3].find((x, i, arr) => { 162 log.push({value: x, index: i, array: [...arr]}); 163 return true; // 返回第一个元素 164 }); 165 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 1 166 }, 167 168 () => { 169 let log = []; 170 const context = {value: 3}; 171 const result = [1, 2, 3, 4].find(function (x, i, arr) { 172 log.push({value: x, index: i, array: [...arr]}); 173 return x === this.value; // 使用 this 进行比较 174 }, context); 175 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 3 176 }, 177 178 () => { 179 let log = []; 180 const result = [{id: 1}, {id: 2}].find((x, i, arr) => { 181 log.push({value: x, index: i, array: [...arr]}); 182 return x.id === 2; 183 }); 184 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: { id: 2 } 185 }, 186 187 () => { 188 let log = []; 189 let obj = {x: 1}; 190 const result = [obj, obj].find((x, i, arr) => { 191 log.push({value: x, index: i, array: [...arr]}); 192 x.x++; // 修改对象属性 193 return x.x > 1; 194 }); 195 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: { x: 2 } 196 }, 197 198 () => { 199 let log = []; 200 const result = [].find((x, i, arr) => { 201 log.push({value: x, index: i, array: [...arr]}); 202 return x === 1; 203 }); 204 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: undefined 205 }, 206 207 () => { 208 let log = []; 209 let arr = [1, 2, 3]; 210 const result = arr.find((x, i, arrRef) => { 211 log.push({value: x, index: i, array: [...arrRef]}); 212 if (i === 1) arrRef.splice(1, 1, 4); // 修改数组 213 return x === 4; 214 }); 215 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 4 216 }, 217]; 218 219findTestCases.forEach((test, i) => { 220 print(`Find Test case ${i + 1}:`); 221 test(); 222 print('---'); 223}); 224