js如何将数组得一部分赋值到另一个位置上,下面web建站小编利用copyWithin
函数实现这个功能!
具体实现代码如下:
复制代码const arr1 = ["小米", "小明", 1, 2, "A", "B"];
//从索引为2个位置开始复制,到索引为4的位置,但不包含5,从索引为1的位置粘贴并覆盖
const newArr = arr1.copyWithin(1, 2, 4);
console.log('输出结果1:', arr1, newArr);
//输出结果1: (6) ['小米', 1, 2, 2, 'A', 'B'] (6) ['小米', 1, 2, 2, 'A', 'B']
//为负数时从后往前数-2从A的位置到-1不包括-1的位置,也就是将A赋值并覆盖到了索引为0的位置
const newArr = arr1.copyWithin(0, -2, -1);
console.log('输出结果2:', newArr); //输出结果2: (6) ['A', 1, 2, 2, 'A', 'B']
//这种结束索引位置在开始索引位置之前的都不生效
const newArr = arr1.copyWithin(0, -2, 2);
const newArr = arr1.copyWithin(0, -2, -4);
console.log('输出结果3:', newArr); //输出结果3: (6) ['A', 1, 2, 2, 'A', 'B']
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
标签: copyWithin, js数组
上面是“js如何将数组得一部分赋值到另一个位置上”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。
当前网址:https://ipkd.cn/webs_14335.html
workflows工作流
令人着迷的一只老虎ComfyUI工作流
一只黑色章鱼ComfyUI工作流
葡萄庄园白葡萄酒ComfyUI工作流
一个美丽的女孩穿着花瓣做的衣服
蘑菇屋,梦幻家园comfyui工作流
一只精致透明的朱红色水晶狐狸
一群可爱的小老鼠ComfyUI工作流
一只白色小猫comfyui工作流
猜你喜欢
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!