1. interface定義數據類型
<script setup lang="ts>
import { reactive ,ref } from 'vue'
//定义数据类型
interface User {
date: string
name: string
address: string
}
const formatter = (row: User) => {
return row.address
}
</script>
2. map & filter
-
map方法
let arr=[3,5,17,15,4,14]; let res1=arr.map((item,index,array)=>{ // return array[index]; //用这种方法也可以获取到当前处理的元素 return item>5; }); console.log(res1) [ false, false, true, true, false, true ] -
filter方法
let arr=[3,5,17,15,4,14]; let res2=arr.filter((item,index,array)=>{ return item>5; }); console.log(res2) [ 17, 15, 14 ]
3. 删除数组中指定元素
-
splice() 方法可以用于添加或删除数组中的元素。
要删除特定元素,首先需要找到该元素的索引,然后使用 splice() 方法删除它。
let array = [1, 2, 3, 4, 5]; let index = array.indexOf(3); // 找到元素3的索引 if (index > -1) { array.splice(index, 1); // 删除索引处的元素 } console.log(array); // 输出: [1, 2, 4, 5] -
filter() 方法创建一个新数组,包含通过所提供函数实现的测试的所有元素。
这可以用来删除特定元素。
let array = [1, 2, 3, 4, 5]; array = array.filter(item => item !== 3); console.log(array); // 输出: [1, 2, 4, 5] -
使用 slice() 和 concat() 方法
使用 slice() 将数组分割成两部分,然后使用 concat() 将它们重新组合,排除掉要删除的元素。
let array = [1, 2, 3, 4, 5]; let index = array.indexOf(3); let newArray = array.slice(0, index).concat(array.slice(index + 1)); console.log(newArray); // 输出: [1, 2, 4, 5] -
使用 pop() 或 shift()
如果需要删除数组的最后一个元素或第一个元素,可以使用 pop() 或 shift() 方法。
let array = [1, 2, 3, 4, 5]; array.pop(); // 删除最后一个元素 console.log(array); // 输出: [1, 2, 3, 4] array.shift(); // 删除第一个元素 console.log(array); // 输出: [2, 3, 4] -
使用 forEach() 和 push()
这种方法涉及遍历数组并将不需要删除的元素推送到新数组中。
let array = [1, 2, 3, 4, 5]; let newArray = []; array.forEach(item => { if (item !== 3) newArray.push(item); ); console.log(newArray); 输出: [1, 2, 4, 5]