当前位置:网站首页>js对数组操作移动进行封装
js对数组操作移动进行封装
2022-07-30 09:28:00 【你的美,让我痴迷】
//1.两个数组元素换位置
export function swapArr(arr, index1, index2) {
arr[index1] = arr.splice(index2, 1, arr[index1])[0];
return arr;
}
//2.将某个元素置顶
export function toFirst(fieldData,index) {
if(index!=0){
// fieldData[index] = fieldData.splice(0, 1, fieldData[index])[0]; 这种方法是与另一个元素交换了位子,
fieldData.unshift(fieldData.splice(index , 1)[0]);
}
}
//3.将指定元素向上移动一格
export function upGo(fieldData,index){
if(index!=0){
fieldData[index] = fieldData.splice(index-1, 1, fieldData[index])[0];
}else{
fieldData.push(fieldData.shift());
}
}
//4.将指定元素向下移动一格
export function downGo(fieldData,index) {
if(index!=fieldData.length-1){
fieldData[index] = fieldData.splice(index+1, 1, fieldData[index])[0];
}else{
fieldData.unshift( fieldData.splice(index,1)[0]);
}
}
//5.数组内两个元素换位子
export function swapArr(arr, index1, index2) {
arr[index1] = arr.splice(index2, 1, arr[index1])[0];
return arr;
}
//6.将数组任意位置的元素移动到末尾
export function toEnd(arr,index) {
arr.push(arr[index]);
arr.splice(index,1);
return arr;
}
//7.数组的去重复值,可以填任意个数组,既可以单个数组去重,也可以多个数组的合并去重
//数组合并去重
export function combine(){
let arr = [].concat.apply([], arguments); //没有去重复的新数组
return Array.from(new Set(arr));
}
//8.删除指定元素,第一个参数为要删除元素的数组,第二个参数为要删除的元素值,第三个值为是否开启严格模式(变量类型严格比较) 例如removeArr([1,2,3,4,5],2,false) 返回[1,3,4,5] 例如removeArr([1,2,3,4,5],2,true) 返回[1,2,3,4,5]
export function removeArr (arr,val,strict) {
let index;
if(strict===true){
index = arr.findIndex(x=>x===value);
}else{
index = arr.findIndex(x=>x==value);
}
if (index > -1) {
arr.splice(index, 1);
}
}
export function swapArr(arr, index1, index2) {
arr[index1] = arr.splice(index2, 1, arr[index1])[0];
return arr;
}
边栏推荐
- Unified exception handling causes ResponseBodyAdvice to fail
- 在机器人行业的专业人士眼里,机器人行业目前的情况如何?
- 北京突然宣布,元宇宙重大消息
- 图像分析:投影曲线的波峰查找
- Flask之路由(app.route)详解
- leetcode 剑指 Offer 10- II. 青蛙跳台阶问题
- Paper reading: SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers
- Oracle 创建和操作表
- 百度推广助手遇到重复关键字,验证错误,怎么一键删除多余的
- 水电表预付费系统
猜你喜欢

PyQt5-用像素点绘制正弦曲线

PyQt5快速开发与实战 8.1 窗口风格

学习笔记11--局部轨迹直接构造法

Security思想项目总结

多线程--线程和线程池的用法

Day113. Shangyitong: WeChat login QR code, login callback interface

Jenkins 如何玩转接口自动化测试?

leetcode 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面

Re21: Read the paper MSJudge Legal Judgment Prediction with Multi-Stage Case Representation Learning in the Real

快解析结合泛微OA
随机推荐
what is this method called
线程池方式开启线程--submit()和execute()的区别
EViews 12.0 software installation package download and installation tutorial
Jenkins 如何玩转接口自动化测试?
元宇宙改变人类工作模式的四种方式
编译报错: undefined reference to `google::FlagRegisterer::FlagRegisterer解决方法
Re17:读论文 Challenges for Information Extraction from Dialogue in Criminal Law
Flink_CDC搭建及简单使用
Shell系统学习之数组
Multithreading--the usage of threads and thread pools
JVM内存布局、类加载机制及垃圾回收机制详解
包、类及四大权限和static
初识Apifox——如何使用Apifox做一个简单的接口测试
Re18: Read the paper GCI Everything Has a Cause: Leveraging Causal Inference in Legal Text Analysis
A new generation of free open source terminal tool, so cool
606. Create a string from a binary tree (video explanation!!!)
企业数字化建设,自研还是采购?
快解析结合用友时空
PyQt5快速开发与实战 8.1 窗口风格
Determine whether a tree is a complete binary tree - video explanation!!!