//数组元素复制并替换
//第一个参数为要替换的位置,正数为从左到右 0 开始 ,负数为从右到左 -1开始
//第二个参数为要复制的元素开始位置,第三个参数为要复制的元素的结束位置
//函数执行后会返回替换后的新数组
let newArray = [9,8,7,6,5,4,3,2,1].copyWithin(-1,0,1);//复制9到1的位置
console.log(newArray);
let newArray1 = [9,8,7,6,5,4,3,2,1].copyWithin(0,8);//复制1到9的位置
console.log(newArray1);
let newArray2 = [9,8,7,6,5,4,3,2,1].copyWithin(0,-1);//复制1到9的位置
console.log(newArray2);
let newArray3 = [9,8,7,6,5,4,3,2,1].copyWithin(8,0,1);//复制9到1的位置
console.log(newArray3);
let newArray4 = [9,8,7,6,5,4,3,2,1].copyWithin(0,-3);//复制3,2,1到替换9,8,7的位置
console.log(newArray4);
let newArray5 = [9,8,7,6,5,4,3,2,1].copyWithin(0,-3,-1);//复制3,2到替换9,8的位置
console.log(newArray5);
let newArray6 = [9,8,7,6,5,4,3,2,1].copyWithin(3,-2);//复制2,1到替换6,5的位置
console.log(newArray6);
let newArray7 = [9,8,7,6,5,4,3,2,1].copyWithin(-3,1,4);//复制8,7,6到替换3,2,1的位置
console.log(newArray7);
console.log([].copyWithin.call([1,2,3,4,5,6,7,8,9],0,-3,-1));//es5写法7,8替换1,2
console.log([1,2,3,4,5,6,7,8,9].find((x)=>x>5));//找出第一个匹配条件的元素并返回该
[1,2,3,4,5,6,7,8,9].find((x,i,a)=>{
if(x>3){
console.log('当前值:',x,'索引:',i,'原始数组:',a);
return x;
}
})//找出第一个匹配条件的元素并返回该
console.log([1,2,3,4,5,6,7,8,9,0].findIndex((v,i,a)=>v<1));//找出数组中第一个小于1的元素的索引
console.log([9,8,7,6,5,4,3,2,1,0].fill(0));//用0填充数组中所有元素
console.log([9,8,7,6,5,4,3,2,1,0].fill(0,1,4));//用0按位置填充数组中的元素
console.log([9,8,7,6,5,4,3,2,1,0].fill(8,-4,-1));//用8按位置填充数组中的元素
console.log([9,8,7,6,5,4,3,2,1,0].fill(8,-3));//用8按位置填充数组中的元素
console.log([9,8,7,6,5,4,3,2,1,0].fill(5,0,3));//用8按位置填充数组中的元素
//数组遍历
let tmpArr = [1,2,3,4,5,6,7,8,9,0];
Object.keys(tmpArr).forEach((v,i,a)=>{ console.log(v)});//遍历键
Object.values(tmpArr).forEach((v,i,a)=>{ console.log(v)});//遍历值
Object.entries(tmpArr).forEach((v,i,a)=>{ console.log(v)});//遍历键值对
//查找数组中是否包含某个值
console.log([1,2,3,4,5,6,7,8,9,0].includes(6,3));
console.log([1,2,3,4,5,6,7,8,9,0].includes(2,3));
console.log(['jack','luck','luzi','make'].includes('luck',1));
let tmpMap = new Map(
[
[0,'A'],
[1,'B'],
[2,'C'],
[3,'D']
]
);
console.log(tmpMap.has(2));//Map检测键
let tmpSet = new Set(
[
'a','b','c','d','e','f','g'
]
);
console.log(tmpSet,tmpSet.has('g'));//Set检测值
let emptyArray = new Array(10);//空值元素
console.log(emptyArray,emptyArray.length);
emptyArray.forEach((v,i,a)=>{
console.log(i,v);
});
//Array不会过滤数组空位
let newTmpArray = Array.from(emptyArray);
newTmpArray.forEach((v,i,a)=>{
console.log(i,v);
});
//下面的几处情况会过滤数组空位
emptyArray.forEach((v,i)=>{
console.log(v);
});
emptyArray.filter((x)=>{
console.log(x);
});
emptyArray.every((x)=>{
console.log(x);
});
emptyArray.some((x)=>{
console.log(x);
});
emptyArray.map((x)=>{
console.log(x);
});
console.log(emptyArray.toString());//toString会打印空位
console.log(emptyArray.join());//join会打印空位
//扩展运算符...会视空位为undefined
console.log(...emptyArray);
//for..of循环中视空位为undefined
for (const ele of emptyArray) {
console.log('空位在for..of中使用,',ele);
}
console.log( 'keys->',emptyArray.keys()) ;
console.log( 'values->',emptyArray.values()) ;
console.log( 'entries->',emptyArray.entries()) ;
console.log( emptyArray.find((x)=>x>0)) ;
console.log( emptyArray.findIndex((x)=>x>0)) ;
//fill函数也会认空位为正常位
console.log(emptyArray.fill('x'));
console.log([,0,,2].copyWithin(1,3));//copyWithin会复制空位
Typescript数组元素操作及空位处理
最新推荐文章于 2025-05-08 13:19:56 发布