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/* 17 * @tc.name:arrayfindIndex 18 * @tc.desc:test Array.findIndex 19 * @tc.type: FUNC 20 * @tc.require: issueI8D8UH 21 */ 22 23(function(){ 24 var a = [0, 1,,3]; 25 var index = a.findIndex(function(val){ 26 return val === undefined; 27 }); 28 print(index); 29})(); 30 31(function () { 32 var array1 = [,]; 33 34 function findIndex() { 35 return array1.findIndex(v => v > 0); 36 } 37 array1.__proto__.push(6); 38 print(findIndex()) 39})(); 40 41{ 42 class C1 { 43 [28262n] = 28262n; 44 } 45 const v2 = new C1(); 46 const v3 = [-4.869758437495864e+307,1000000000000.0]; 47 function f4(a5, a6) { 48 for (const v7 in v2) { 49 } 50 a6(); 51 return a6; 52 } 53 try { v3.findIndex(f4);} catch (err) { print(err)}; 54} 55 56const findIndexTestCases = [ 57 () => { 58 let log = []; 59 const result = [1, 2, 3, 4].findIndex((x, i, arr) => { 60 log.push({ value: x, index: i, array: [...arr] }); 61 return x === 3; 62 }); 63 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 2 64 }, 65 66 () => { 67 let log = []; 68 const result = [1, 2, 3, 4].findIndex((x, i, arr) => { 69 log.push({ value: x, index: i, array: [...arr] }); 70 return x === 5; 71 }); 72 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: -1 73 }, 74 75 () => { 76 let log = []; 77 const result = [].findIndex((x, i, arr) => { 78 log.push({ value: x, index: i, array: [...arr] }); 79 return x > 0; 80 }); 81 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: -1 82 }, 83 84 () => { 85 let log = []; 86 const result = [false, 0, ''].findIndex((x, i, arr) => { 87 log.push({ value: x, index: i, array: [...arr] }); 88 return !x; // 查找第一个 falsy 值 89 }); 90 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 0 91 }, 92 93 () => { 94 let log = []; 95 const result = [1, 2, 3, 4].findIndex((x, i, arr) => { 96 log.push({ value: x, index: i, array: [...arr] }); 97 return x > 2; // 第一个大于 2 的元素 98 }); 99 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 2 100 }, 101 102 () => { 103 let log = []; 104 const result = [null, 1, 2].findIndex((x, i, arr) => { 105 log.push({ value: x, index: i, array: [...arr] }); 106 return x !== null && x !== undefined; // 查找非 null 和 undefined 的值 107 }); 108 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 1 109 }, 110 111 () => { 112 let log = []; 113 let arr = [1, 2, 3]; 114 const result = arr.findIndex((x, i, arrRef) => { 115 log.push({ value: x, index: i, array: [...arrRef] }); 116 if (i === 1) arrRef.push(4); // 修改数组 117 return x === 3; 118 }); 119 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 2 120 }, 121 122 () => { 123 let log = []; 124 const result = [NaN, 1, 2].findIndex((x, i, arr) => { 125 log.push({ value: x, index: i, array: [...arr] }); 126 return !isNaN(x); // 查找第一个非 NaN 元素 127 }); 128 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 1 129 }, 130 131 () => { 132 let log = []; 133 const result = [1, 2, 3].findIndex((x, i, arr) => { 134 log.push({ value: x, index: i, array: [...arr] }); 135 return false; // 所有值都不满足条件 136 }); 137 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: -1 138 }, 139 140 () => { 141 let log = []; 142 const result = [1, 2, 3].findIndex((x, i, arr) => { 143 log.push({ value: x, index: i, array: [...arr] }); 144 return true; // 第一个元素匹配 145 }); 146 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 0 147 }, 148 149 () => { 150 let log = []; 151 const context = { value: 3 }; 152 const result = [1, 2, 3, 4].findIndex(function(x, i, arr) { 153 log.push({ value: x, index: i, array: [...arr] }); 154 return x === this.value; // 使用 this 进行比较 155 }, context); 156 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 2 157 }, 158 159 () => { 160 let log = []; 161 const result = [{ id: 1 }, { id: 2 }].findIndex((x, i, arr) => { 162 log.push({ value: x, index: i, array: [...arr] }); 163 return x.id === 2; // 查找 id 为 2 的对象 164 }); 165 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 1 166 }, 167 168 () => { 169 let log = []; 170 let obj = { x: 1 }; 171 const result = [obj, obj].findIndex((x, i, arr) => { 172 log.push({ value: x, index: i, array: [...arr] }); 173 x.x++; // 修改对象属性 174 return x.x > 1; // 查找对象属性大于 1 175 }); 176 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 0 177 }, 178 179 () => { 180 let log = []; 181 const result = [].findIndex((x, i, arr) => { 182 log.push({ value: x, index: i, array: [...arr] }); 183 return x === 1; 184 }); 185 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: -1 186 }, 187 188 189 () => { 190 let log = []; 191 let arr = [1, 2, 3]; 192 const result = arr.findIndex((x, i, arrRef) => { 193 log.push({ value: x, index: i, array: [...arrRef] }); 194 if (i === 1) arrRef.splice(1, 1, 4); // 修改数组 195 return x === 4; // 查找新元素 4 196 }); 197 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 1 198 }, 199 200 () => { 201 let log = []; 202 const result = [[1], [2], [3]].findIndex((x, i, arr) => { 203 log.push({ value: x, index: i, array: [...arr] }); 204 return x[0] === 2; // 查找数组中嵌套数组值为 2 的索引 205 }); 206 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 1 207 }, 208 209 () => { 210 let log = []; 211 const result = [1, 2, 3, -1, 5].findIndex((x, i, arr) => { 212 log.push({ value: x, index: i, array: [...arr] }); 213 return x < 0; 214 }); 215 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 3 216 }, 217 218 () => { 219 let log = []; 220 const result = ['apple', 'banana', 'cherry'].findIndex((x, i, arr) => { 221 log.push({ value: x, index: i, array: [...arr] }); 222 return x === 'banana'; 223 }); 224 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 1 225 }, 226 227 () => { 228 let log = []; 229 const result = [{ name: 'Alice' }, { name: 'Bob' }].findIndex((x, i, arr) => { 230 log.push({ value: x, index: i, array: [...arr] }); 231 return x.name === 'Bob'; 232 }); 233 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 1 234 }, 235 236 () => { 237 let log = []; 238 const result = [{ age: 30 }, { age: 25 }, { age: 30 }].findIndex((x, i, arr) => { 239 log.push({ value: x, index: i, array: [...arr] }); 240 return x.age === 30; 241 }); 242 print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: 0 243 }, 244]; 245 246findIndexTestCases.forEach((test, i) => { 247 print(`FindIndex Test case ${i + 1}:`); 248 test(); 249 print('---'); 250}); 251