From b86d74499eafa0ba3c7d4cb16ced8006ab6e7226 Mon Sep 17 00:00:00 2001 From: mahaifeng Date: Wed, 4 Sep 2024 12:36:43 +0800 Subject: [PATCH] =?UTF-8?q?[array]=E5=8E=BB=E9=99=A4=E6=89=8B=E5=8A=A8?= =?UTF-8?q?=E7=94=9F=E6=88=90=E7=9A=84=E4=BB=A3=E7=A0=81(=E5=8E=BB?= =?UTF-8?q?=E6=8E=89=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/uts/buildin-object-api/array.md | 68 ++++------------------------ 1 file changed, 10 insertions(+), 58 deletions(-) diff --git a/docs/uts/buildin-object-api/array.md b/docs/uts/buildin-object-api/array.md index a7a63f9f..78756465 100644 --- a/docs/uts/buildin-object-api/array.md +++ b/docs/uts/buildin-object-api/array.md @@ -927,74 +927,27 @@ val convertArrayFromKotlin = UTSArray.fromNative(kotlinArray); - 创建数组 - 通过索引访问数组元素 -```ts -const first = fruits[0] -// Apple -const last = fruits[fruits.length - 1] -// Banana -``` + - 遍历数组 -```ts -fruits.forEach(function(item, index, array) { - console.log(item, index) -}) -// Apple 0 -// Banana 1 -``` + - 注意:数组遍历不推荐使用 for in 语句,因为在 ts 中 for in 遍历的是数组的下标,而在 Swift 和 Kottlin 中遍历的是数组的元素,存在行为不一致。 - 添加元素到数组的末尾 -```ts -const newLength = fruits.push('Orange') -// ["Apple", "Banana", "Orange"] -``` + - 删除数组末尾的元素 -```ts -const last = fruits.pop() // remove Orange (from the end) -// ["Apple", "Banana"] -``` + - 删除数组头部元素 -```ts -const first = fruits.shift() // remove Apple from the front -// ["Banana"] -``` + - 添加元素到数组的头部 -```ts -const newLength = fruits.unshift('Strawberry') // add to the front -// ["Strawberry", "Banana"] -``` + - 找出某个元素在数组中的索引 -```ts -fruits.push('Mango') -// ["Strawberry", "Banana", "Mango"] -const pos = fruits.indexOf('Banana') -// 1 -``` + - 通过索引删除某个元素 -```ts -const removedItem = fruits.splice(pos, 1) // this is how to remove an item -// ["Strawberry", "Mango"] -``` + - 从一个索引位置删除多个元素 -```ts -const vegetables = ['Cabbage', 'Turnip', 'Radish', 'Carrot'] -console.log(vegetables) -// ["Cabbage", "Turnip", "Radish", "Carrot"] -const pos = 1 -const n = 2 -const removedItems = vegetables.splice(pos, n) -// this is how to remove items, n defines the number of items to be removed, -// starting at the index position specified by pos and progressing toward the end of array. -console.log(vegetables) -// ["Cabbage", "Carrot"] (the original array is changed) -console.log(removedItems) -// ["Turnip", "Radish"] -``` + - 复制一个数组 -```ts -const shallowCopy = fruits.slice() // this is how to make a copy -// ["Strawberry", "Mango"] -``` + ### 访问数组元素 数组的索引是从 0 开始的,第一个元素的索引为 0,最后一个元素的索引等于该数组的 长度 减 1。 @@ -1002,7 +955,6 @@ const shallowCopy = fruits.slice() // this is how to make a copy 如果指定的索引是一个无效值,将会抛出 IndexOutOfBoundsException 异常 下面的写法是错误的,运行时会抛出 SyntaxError 异常,而原因则是使用了非法的属性名: - ```ts console.log(arr.0) // a syntax error ``` -- GitLab