当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

leetcode 剑指 Offer 47. 礼物的最大价值

Re17:读论文 Challenges for Information Extraction from Dialogue in Criminal Law

Do you really understand the 5 basic data structures of Redis?

ThreadLocal内存泄漏是伪命题?

A near-perfect Unity full-platform hot update solution

实战演练 | 在 MySQL 中计算每日平均日期或时间间隔

Security思想项目总结

69. Sqrt(x)x 的平方根

Practical Walkthrough | Calculate Daily Average Date or Time Interval in MySQL

使用 Neuron 接入 Modbus TCP 及 Modbus RTU 协议设备
随机推荐
你真的懂Redis的5种基本数据结构吗?
Matplotlib--绘图标记
Flink_CDC搭建及简单使用
Re18: Read the paper GCI Everything Has a Cause: Leveraging Causal Inference in Legal Text Analysis
Basic operations of sequence table in C language
what is this method called
idea2021+Activiti【最完整笔记一(基础使用)】
元宇宙改变人类工作模式的四种方式
debian10 install djando
echart图表清空上一次数据
Domino服务器SSL证书安装指南
A new generation of free open source terminal tool, so cool
leetcode 剑指 Offer 10- II. 青蛙跳台阶问题
团队级敏捷真的没你想的那么简单
Redis Desktop Manager 2022.4.2 released
多线程保证单个线程开启事务并生效的方案
【HMS core】【FAQ】HMS Toolkit典型问题合集1
Test automation selenium (a)
图像分析:投影曲线的波峰查找
PyQt5-在窗口上绘制文本