Typescript数组元素操作及空位处理

本文详细介绍了JavaScript中的数组操作方法,包括`copyWithin`、`find`、`findIndex`、`fill`、`includes`以及`Map`和`Set`的使用。通过实例展示了如何替换数组元素、查找特定元素、填充数组以及遍历和检测数组和集合中的元素。还探讨了空位在不同操作中的处理方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


//数组元素复制并替换
//第一个参数为要替换的位置,正数为从左到右 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会复制空位

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

自由软件开发者

有你的鼓励,我会更加努力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值