0
点赞
收藏
分享

微信扫一扫

js数组操作包

敬亭阁主 2022-04-29 阅读 60

前言

提示:这里可以添加本文要记录的大概内容:

自己按照自己的习惯分类封装了一些js对数组的操作方法

提示:直接上代码

1.封装代码

//往数组里添加内容
//0为头部,-1为尾部,其他长度
function addlist(list,kind,element,place) {//参数:数组、类型、元素、位置
	if(kind==0){
		return list.unshift(element)
	}else if(kind==-1){
		return list.push(element)
	}else{
		return list.splice(place,0,element)
	}
} 
//删除数组里指定内容
//0为删除数组头部、-1为删除数组尾部、1为删除指定位置元素、-2为删除匹配元素
function deletelist(list,kind,element,place){//参数:数组、类型、元素(个数)、位置
	if(kind==0){
		return list.shift()
	}else if(kind==-1){
		return list.pop()
	}else if(kind==1){
		return list.splice(place,1)
	}else if(kind==-2){
		return list.splice(list.indexOf(element),1)
	}
}
//修改数组里指定内容
//0为用指定符号链接数组、1为数组小到大排序、2为数组大到小排序、3为逆转数组、4为合并数组
function upadtelist(list,kind,data){//参数:数组、类型、数据
	if(kind==0){
		return list.join(data)
	}else if(kind==1){
		return list.sort()
	}else if(kind==2){
		return list.sort().reverse()
	}else if(kind==3){
		return list.reverse()
	}else if(kind==4){
		return list.concat(data)
	}
} 
//查找数组里指定内容
//0为查找数组中是否含有某元素、1为查找数组中元素位置、2为判断变量是否为数组
function searchlist(list,kind,element){//参数:数组、类型、数据
	if(kind==0){
		return list.includes(element)//返回值为true和false
	}else if(kind==1){
		return list.indexOf(element)
	}else if(kind==2){
		return Array.isArray(list)
	}
}

export {
addlist,
deletelist,
searchlist,
upadtelist
}

2.页面使用

代码如下(示例):

let myJs = require("@/js/list.js");
console.log(myJs.deletelist(this.ddl,-2,3))

总结

提示:这里对文章进行总结:
方法分类是赞找自己的习惯,没有总结的全面

举报

相关推荐

0 条评论