js 数组插入和删除处理
function insertArray(arr, val, compare, maxLen) {
//返回位置
const index = arr.findIndex(compare)
if (index === 0) {
return
}
if (index > 0) {
//删除一个
arr.splice(index, 1)
}
//再插入(unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度)
arr.unshift(val)
if (maxLen && arr.length > maxLen) {
//pop() 方法用于删除并返回数组的最后一个元素。
arr.pop()
}
}
删除
function deleteFromArray(arr, compare) {
const index = arr.findIndex(compare)
if (index > -1) {
arr.splice(index, 1)
}
}
js 数组插入和删除处理的更多相关文章
- javascript 常见数组操作( 1、数组整体元素修改 2、 数组筛选 3、jquery 元素转数组 4、获取两个数组中相同部分或者不同部分 5、数组去重并倒序排序 6、数组排序 7、数组截取slice 8、数组插入、删除splice(需明确位置) 9、数组遍历 10、jQuery根据元素值删除数组元素的方)
主要内容: 1.数组整体元素修改 2. 数组筛选 3.jquery 元素转数组 4.获取两个数组中相同部分或者不同部分 5.数组去重并倒序排序 6.数组排序 7.数组截取slice 8.数组插入.删除 ...
- js 给json添加新的字段,或者添加一组数据,在JS数组指定位置删除、插入、替换元素
JS定义了一个json数据var test={name:"name",age:"12"};需要给test再添加一个字段,需要什么办法,可以让test的值为{na ...
- js数组依据下标删除元素
最近在项目中遇到了一些问题,基础性的东西记得不牢固,就总结一下放在这里备再次查找,对操作js数组的一些问题一些常用的记录! 1.创建数组 var array = new Array(); var ar ...
- js数组插入指定位置元素,删除指定位置元素,查找指定位置元素算法
将元素x插入到顺序表L(数组)的第i个数据元素之前 function InsertSeqlist(L, x, i) { // 将元素x插入到顺序表L的第i个数据元素之前 if(L.length == ...
- js数组添加或删除元素
var arr = new Array(); arr[] = "aaa"; arr[] = "bbb"; arr[] = "ccc"; ar ...
- js 数组 添加或删除 元素 splice 创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素 filter
里面可以用 箭头函数 splice 删除 增加 数组 中元素 操作数组 filter 创建新数组 检查指定数组中符合条件的所有元素
- js 数组中如何删除字典
区别: []表示是一个数组,如var strs = ['a','b','c'].{}表示是一个对象,比如,var obj = {name: '宙斯',sex: 1} 如何在数组中删除指定对象呢?? [ ...
- js 数组方法比较
js 数组方法比较 table th:first-of-type { width: 80px; } table th:nth-of-type(2) { width: 120px; } table th ...
- JS数组方法汇总 array数组元素的添加和删除
js数组元素的添加和删除一直比较迷惑,今天终于找到详细说明的资料了,先给个我测试的代码^-^ var arr = new Array(); arr[0] = "aaa"; arr[ ...
随机推荐
- CROS+node-basis+ajax
$.ajax({ url: this.baseUrl + this.restful.showDesigerViewList, type: "post", dataType: &qu ...
- 简单工厂模式demo
1. 简单工厂模式 domain的接口 public interface Color{ public void display(); } red public Class Red implements ...
- R语言之Apriori算法
---恢复内容开始--- 1.概念 关联分析:用于发现隐藏在大型数据集中的有意义的联系 项集:0或多个项的集合.例如:{啤酒,尿布,牛奶,花生} 是一个4-项集,意义想象成爸爸去超市买啤酒和花生,给儿 ...
- [转] Quality Of Service In OpenStack
http://tropicaldevel.wordpress.com/2013/07/15/quality-of-service-in-openstack/ In this post I will b ...
- [Swift]LeetCode86. 分隔链表 | Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- [Swift]LeetCode113. 路径总和 II | Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [Swift]LeetCode741. 摘樱桃 | Cherry Pickup
In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 mea ...
- [Swift]LeetCode754. 到达终点数字 | Reach a Number
You are standing at position 0 on an infinite number line. There is a goal at position target. On ea ...
- mysql-proxy负载均衡
1.下载mysql-proxy包 2.解压并启动 tar zxvf mysql-proxy....tar mv mysql-proxy... /user/local/proxy cd /usr/loc ...
- Mongodb的集合插入一个字段
在mongodb的现有集合中增加一个字段: db.rules.update({"_id" : ObjectId("587e5e0e09a4670334208e88&quo ...