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