• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 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@Entry
17@Component
18struct MyComponent2 {
19    @State arr: number[] = [10, 20, 30]
20
21    build() {
22        Column() {
23            Button() {
24                Text('Reverse Array')
25                    .fontSize(30)
26            }.onClick(() => {
27                this.arr.reverse()
28            }).width(80).height(40)
29            Button() {
30                Text('clear Array')
31                    .fontSize(30)
32            }.onClick(() => {
33                this.arr.splice(0, 1)
34            }).width(80).height(40)
35            Button() {
36                Text('replace Array item')
37                    .fontSize(30)
38            }.onClick(() => {
39                this.arr.splice(0, 1, 40)
40            }).width(80).height(40)
41            Button() {
42                Text('replace Array')
43                    .fontSize(30)
44            }.onClick(() => {
45                let newArr = [30, 40, 50]
46                this.arr = newArr
47            }).width(80).height(40)
48
49            ForEach(this.arr,                         // Parameter 1: array to be iterated
50                (item: number) => {               // Parameter 2: item generator
51                    Text(`item value: ${item}`)
52                        .fontSize(20)
53                },
54                (item: number) => item.toString() // Parameter 3: unique key generator, which is optional but recommended.
55            )
56        }.height(800)
57    }
58}
59