1/* 2 * Copyright (c) 2024 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/* 18 * @tc.name: forof_map 19 * @tc.desc: test map forof 20 * @tc.type: FUNC 21 */ 22const combinedMap = new Map([ 23 ["ark_stringKey", "ark_stringValue"], 24 [1, "ark_numberValue"], 25 [true, "ark_booleanValue"], 26 [{}, "ark_objectValue"], 27 [null, "ark_nullValue"], 28 [undefined, "ark_undefinedValue"], 29 [NaN, "ark_NaNValue"], 30 [Infinity, "ark_infinityValue"], 31 [-Infinity, "ark_negativeInfinityValue"], 32 [RegExp("ark_regexKey"), "ark_regexValue"], 33 [new Map(), "ark_mapValue"], 34 [new Set(), "ark_setValue"], 35 [Array.from([1, 2, 3]), "ark_arrayValue"], 36 ["ark_unicodeKey ", "ark_unicodeValue "] 37]); 38 39for (const key of combinedMap.keys()) { 40 print(key); 41} 42 43for (const value of combinedMap.values()) { 44 print(value); 45} 46 47for (const entries of combinedMap.entries()) { 48 print(entries); 49} 50 51let keyIterator = combinedMap.keys(); 52let valueIterator = combinedMap.values(); 53let entriesIterator = combinedMap.entries(); 54 55combinedMap.set("ark_stringKey", "ark_stringValueUpdated"); 56print(valueIterator.next().value); 57 58combinedMap.delete("ark_stringKey"); 59print(keyIterator.next().value); 60 61print(entriesIterator.next().value); 62print(entriesIterator.next().value); 63 64// update map when getting iterator next 65let iterResult; 66while (!(iterResult = entriesIterator.next()).done) { 67 print(iterResult.value); 68 69 if (iterResult.value[0] == undefined) { 70 combinedMap.clear(); 71 } 72} 73 74class MyMap extends Map { 75 constructor() { 76 super(); 77 } 78} 79 80const newMap = new MyMap([ 81 [1, 1], 82 [2, 2], 83 ['updatepoint1', 'updatepoint1'], 84 [3, 3], 85 [4, 4], 86 ['updatepoint2', 'updatepoint2'], 87 [5, 5], 88 [6, 6], 89]); 90 91for (let value of newMap.values()) { 92 print(value); 93 if (value == 'updatepoint1') { 94 // refore updatepoint1 95 newMap.set(2, 3); 96 } 97 98 if (value == 'updatepoint2') { 99 // after pdatepoint2 100 newMap.set(5, 10); 101 newMap.delete(6); 102 } 103} 104 105