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 16const words = ['spray', 'elite', 'exuberant', 'destruction', 'present']; 17 18const result = words.filter((word) => word.length > 6); 19 20print(result); 21let wordss = ["spray", "limit", "exuberant", "destruction", "elite", "present"]; 22 23const modifiedWords = wordss.filter((word, index, arr) => { 24 arr[index + 1] += " extra"; 25 return word.length < 6; 26}); 27 28print(modifiedWords); 29 30const words1 = ["spray", "limit", "exuberant", "destruction", "elite", "present"]; 31const appendedWords = words1.filter((word, index, arr) => { 32 arr.push("new"); 33 return word.length < 6; 34}); 35print(appendedWords); 36const words2 = ["spray", "limit", "exuberant", "destruction", "elite", "present"]; 37const deleteWords = words2.filter((word, index, arr) => { 38 arr.pop(); 39 return word.length < 6; 40}); 41 42print(deleteWords); 43 44const arrayLike = { 45 length: 3, 46 0: "a", 47 1: "b", 48 2: "c", 49}; 50print(Array.prototype.filter.call(arrayLike, (x) => x <= "b")); 51 52const fruits = ["apple", "banana", "grapes", "mango", "orange"]; 53function filterItems(arr, query) { 54 return arr.filter((el) => el.toLowerCase().includes(query.toLowerCase())); 55} 56 57print(filterItems(fruits, "ap")); // ['apple', 'grapes'] 58print(filterItems(fruits, "an")); // ['banana', 'mango', 'orange'] 59 60const array = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; 61 62function isPrime(num) { 63 for (let i = 2; num > i; i++) { 64 if (num % i === 0) { 65 return false; 66 } 67 } 68 return num > 1; 69} 70 71print(array.filter(isPrime)); // [2, 3, 5, 7, 11, 13] 72 73var array1=[,] 74function filter(){ 75 return array1.filter(v => v>0 ); 76} 77array1.__proto__.push(6); 78var narr = filter(); 79print(JSON.stringify(Object.getOwnPropertyDescriptor(narr,0))); 80 81var bPar = true; 82var bCalled = false; 83 84function callbackfn(val, idx, obj) 85{ 86 bCalled = true; 87 if (obj[idx] !== val) 88 bPar = false; 89} 90 91var srcArr = [0, 1, true, null, new Object(), "five"]; 92srcArr[9999999] = -6.6; 93var resArr = srcArr.filter(callbackfn); 94 95 96print(bCalled); 97print(bPar); 98 99const emptyArr = []; 100print(emptyArr.filter(isPrime).length); 101 102{ 103 const arr1 = [1, null, 3, undefined, 5, null]; 104 const result1 = arr1.filter(item => item != null); 105 print(result1); // [1, 3, 5] 106 107 const arr2 = [1, 2, 3, 4, 5]; 108 const result2 = arr2.filter((num, index) => { 109 if (index === 2) return false; 110 return true; 111 }); 112 print(result2); // [1, 2, 4, 5] 113 114 const arr3 = [0, 'hello', NaN, '', false, 42, undefined]; 115 arr3.filter((item, index) => { 116 if (!item) { 117 arr3.splice(index, 1); 118 return false; 119 } 120 return true; 121 }); 122 print(arr3); // ['hello', 42] 123 124 const arr4 = [1, 2, 3, 4, 5]; 125 const result4 = arr4 126 .map((num, index, array) => { 127 if (index === 3) array.pop(); 128 return num * 2; 129 }) 130 .filter(num => num !== 6); 131 print(result4); // [2, 4, 8] 132} 133