当前位置:网站首页>Js array operating mobile for encapsulation
Js array operating mobile for encapsulation
2022-07-30 10:14:00 【Your beauty fascinates me】
//1.Swap two array elements
export function swapArr(arr, index1, index2) {
arr[index1] = arr.splice(index2, 1, arr[index1])[0];
return arr;
}
//2.Brings an element to the top
export function toFirst(fieldData,index) {
if(index!=0){
// fieldData[index] = fieldData.splice(0, 1, fieldData[index])[0]; This method swaps places with another element,
fieldData.unshift(fieldData.splice(index , 1)[0]);
}
}
//3.Moves the specified element up one space
export function upGo(fieldData,index){
if(index!=0){
fieldData[index] = fieldData.splice(index-1, 1, fieldData[index])[0];
}else{
fieldData.push(fieldData.shift());
}
}
//4.Moves the specified element down one space
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.Swap two elements in an array
export function swapArr(arr, index1, index2) {
arr[index1] = arr.splice(index2, 1, arr[index1])[0];
return arr;
}
//6.Move an element anywhere in the array to the end
export function toEnd(arr,index) {
arr.push(arr[index]);
arr.splice(index,1);
return arr;
}
//7.Array of deduplicated values,Any number of arrays can be filled,Either a single array can be deduplicated,It is also possible to merge multiple arrays to remove duplicates
//数组合并去重
export function combine(){
let arr = [].concat.apply([], arguments); //没有去重复的新数组
return Array.from(new Set(arr));
}
//8.删除指定元素,The first parameter is the array whose elements are to be removed,The second parameter is the element value to delete,The third value is whether strict mode is enabled(Variable types are strictly compared) 例如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;
}
边栏推荐
猜你喜欢
Container Technology - A Simple Understanding of Kubernetes Objects
JCL 学习
线上靶机prompt.ml
idea2021+Activiti [the most complete note one (basic use)]
A new generation of free open source terminal tool, so cool
idea2021+Activiti【最完整笔记一(基础使用)】
Matplotlib--绘图标记
(***重点***)Flink常见内存问题及调优指南(一)
leetcode 剑指 Offer 12. 矩阵中的路径
Re16:读论文 ILDC for CJPE: Indian Legal Documents Corpus for Court Judgment Prediction and Explanation
随机推荐
快解析结合友加畅捷通t1飞跃版
shell script
Four ways the Metaverse is changing the way humans work
Re21: Read the paper MSJudge Legal Judgment Prediction with Multi-Stage Case Representation Learning in the Real
SST-Calib:结合语义和VO进行时空同步校准的lidar-visual外参标定方法(ITSC 2022)
最长公共序列、串问题总结
Soft Exam System Architect Concise Tutorial | Case Analysis | Requirement Analysis
js对数组操作移动进行封装
Flask's routing (app.route) detailed
Re15:读论文 LEVEN: A Large-Scale Chinese Legal Event Detection Dataset
唯物辩证法-条件论
大数据产品:标签体系0-1搭建实践
Version management of public Jar packages
C语言顺序表基本操作
C#中Config文件中,密码的 特殊符号的书写方法。
Materialist Dialectics - Conditionalism
快解析结合象过河erp
Test automation selenium (a)
In the robot industry professionals, Mr Robot industry current situation?
By building a sequence table - teach you to calculate time complexity and space complexity (including recursion)