首页 > 其他分享 >数组操作

数组操作

时间:2024-02-01 18:11:27浏览次数:14  
标签:const name 元素 includes 数组 操作 id

  • filter
const a = [{"id":"1","name":"张三"},{"id":"2","name":"李四"}]
a.filter(it => it.id === "1")   // [{"id":"1","name":"张三"}]
a.filter(it => it.aaa === "1")   或者  a.filter(it => it.id === "3")   // []
  • map
const a = [{"id":"1","name":"张三"},{"id":"2","name":"李四"}]
a.map(it=>it.id)  // ['1', '2']
a.map(it=>it.aaa) // [undefined, undefined]
a.map(it=>it.id==="1") // [true, false]

 

  • find
语法:array.find(function(currentValue, index, arr),thisValue)
currentValue 必需。当前元素
index 可选。当前元素的索引值
arr 可选。当前元素所属的数组对象
 
const a = [{"id":"1","name":"张三"},{"id":"2","name":"李四"}]
箭头函数
a.find(it=>it.id==='1') // {id: '1', name: '张三'}
a.find(it=>it.id==='3') 或者 a.find(it=>it.aaa==='1') // undefined
  • every
const a = [{"id":"1","name":"张三"}]
a.every(it=>it.id==='1') // true
const a = [{"id":"1","name":"张三"},{"id":"2","name":"李四"}]
a.every(it=>it.id==='1') // false
  • unshift 将指定的数值添加到数组的开头
const a = ['1','2','3']
a.unshift('-1') // 4
a // ['-1','1','2','3']
  • push 将指定的数值添加到数组的尾部
const a = ['1','2','3']
a.push('4') // 4
a // ['1','2','3','4']
  • .pop 删除数组最后一个元素
const a = ['1','2','3']
a.pop() // '3'
a // ['1', '2']
  • .shift 删除数组第一个元素
const a = ['1','2','3']
a.shift() // '1'
a // ['2','3']
  • .splice
(1)纯删除   数组.arr.splice(起始下标,删除数量)
    从起始下标开始,删除几个
(2)添加    数组.arr.splice(起始下标,删除数量,...插入元素)
   从起始下标开始,删除几个,并在该位置添加插入的元素
返回值 : 返回的是刚才删除的元素.
// 删除
const a = ['1','2','3']
a.splice(1,1) // ['2']
a // ['1','3']
 
//  删除并插入
const a = ['1','2','3']
a.splice(1, 1, '2-1', '2-2') // ['2']
a // ['1', '2-1', '2-2', '3']
  • contact 两个数组合并
const a = ['1','2','3']
const b = ['2','3','4']
a.concat(b) // ['1', '2', '3', '2', '3', '4']
a // ['1', '2', '3']
b // ['2', '3', '4']
Array.from(new Set(a.concat(b))) // 去重 ['1', '2', '3', '4']
a.concat('7',b) // ['1', '2', '3', '7', '2', '3', '4']
a.concat('7'+b) // ['1', '2', '3', '72,3,4']
a + b // '1,2,32,3,4'
  • join 数组转字符串
const a = ['1','2','3']
a.join('-') // '1-2-3'
  • reverse 数组顺序颠倒
const a = ['1','2','3']
a.reversed() // ['3', '2', '1']
  • sort 排序
const a = [{"id":"1","name":"张三"},{"id":"2","name":"李四"}]
a.sort(function(a,b) {
   return a.id-b.id
}) // 升序 [{"id":"1","name":"张三"},{"id":"2","name":"李四"}]
a.sort(function(a,b) {
   return b.id-a.id
}) // 降序 [{"id":"2","name":"李四"},{"id":"1","name":"张三"}]
  • reduce 为每一个元素执行一次回调,并得到结果
语法:array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
total 必需。初始值, 或者计算结束后的返回值。
currentValue 必需。当前元素
currentIndex 可选。当前元素的索引
arr 可选。当前元素所属的数组对象。
initialValue 可选。传递给函数的初始值

const a = [{"id":"1","value":1},{"id":"2","value":2}]
a.reduce((sum,num)=>{
    return sum + num.value
},10) // sum初始值是10
10+1+2 // 13
 
const a = [{"id":"1","value":1},{"id":"2","value":2}]
a.reduce((obj,num)=>{
    obj.push(num.id);
    return obj
}, []) // ['1', '2']
 
// 当元素是reduce函数的输出时,不能给元素赋值
  • slice 数组复制
语法:slice(start,end)
start 参数必须(不包含),规定从何处开始选取。如果是负数,那么它规定从数组尾部开始算起的位置
end 参数不必须(包含),规定从何处结束选取
let a = [1,2,3,4,5,8,9,6,2,4,5]
a.slice(3) // [4, 5, 8, 9, 6, 2, 4, 5]
a.slice(3, 6) // [4, 5, 8]
a.slice(-3) // [2, 4, 5]
a.slice(-3, -1) // [2, 4]
  • includes
语法:slice(searchElement, ?fromIndex)
searchElement 参数必须,查询的内容
fromIndex 参数可选,起始下标,可为负数,负数代表从尾部开始查
const a = ['1','2','3','4','5']
a.includes('1') // true
a.includes('1', 0) // true
a.includes('1', 1) // false
a.includes('4', -1) // false
a.includes('4', -2) // ture

 

// 与findindex相比,如果数组中存在NaN,findindex会误判,includes不会
const a = ['1','2','3','4','5',NaN]
a.findIndex(it => it === NaN) // -1 a.includes(NaN) // -1 a.findIndex(it => isNaN(it)) // 5 findindex解决方案    

标签:const,name,元素,includes,数组,操作,id
From: https://www.cnblogs.com/lzh123/p/18001812

相关文章

  • pandas - isin()函数 是一个pandas.Series和pandas.DataFrame的方法,用于检查每个元素
    matched_rows=df[~df['设备IMEI'].isin(b_df['设备IMEI'])]这段代码的作用是从DataFramedf中筛选出不在另一个DataFrameb_df的"设备IMEI"列中的值。df['设备IMEI']表示在DataFramedf中获取"设备IMEI"列的序列。b_df['设备IMEI']表示在DataFrameb_df......
  • C# 自己写的编码机制,将任意字节数组和可打印中文字符串相互转换
    正常情况下咱们可以用Base64将字节数组转成可打印字符串,但是有的时候咱们需要编码后具有一定的保密性,很明显Base64就不适用了,网上有个与熊论道就挺有意思的,于是我也研究学习了下,自己实现了一个将字节流编码为可打印(可拷贝)中文字符串的功能,反之,也能将中文字符串解码为原始字节流......
  • leedcode 合并两个有序数组 切片 原地修改
    使用nums1[:m+n]=nums1_new时,这是在原地修改nums1列表。具体来说,这个语句使用切片将nums1中前m+n个元素替换为nums1_new中的元素。这样做的结果是,nums1的原始内存空间被修改,而不是创建一个新的列表对象。使用nums1=nums1_new,这将创建一个新的列表对象,并让nu......
  • concurrent hashmap put操作的线程安全
     减小锁粒度:将Node链表的头节点作为锁,若在默认大小16情况下,将有16把锁,大大减小了锁竞争(上下文切换),就像开头所说,将串行的部分最大化缩小,在理想情况下线程的put操作都为并行操作。同时直接锁住头节点,保证了线程安全Unsafe的getObjectVolatile方法:此方法确保获取到的值为最新  ......
  • Python3 操作ES7
    Python3操作ES7 Python3操作ES71、连接ES7,创建索引fromelasticsearchimportElasticsearches=Elasticsearch(['127.0.0.1:9200'])index_name='student'request_body={"mappings":{"properties":{......
  • 【操作系统和计网从入门到深入】(八)线程
    复习八·线程1.如何理解线程只要满足,比进程轻量化,cpu内所有线程资源共享,创建维护成本更低等要求,就能叫线程。不同的OS实现方式不同,下面这个是Linux特有的方案。Linux没有给线程重新设计数据结构!什么叫做进程?pcb+地址空间+页表CPU调度的基本单位:线程!2.开始使用pthre......
  • UnsupportedOperationException------“不支持的操作异常”
    在Java编程语言中,当你试图调用一个当前环境或对象不支持的方法时,就会抛出这个异常。例如:调用不可变集合(如通过Collections.unmodifiableList()创建的列表)的添加、删除或修改方法。使用Arrays.asList()返回的对象尝试修改其内容。在实现了某个接口但未提供所有必需实现方法的类......
  • 区间修改,单点查询的树状数组
    #include<bits/stdc++.h>#defineCLOSEios::sync_with_stdio(false);cin.tie(0);cout.tie(0)#defineendl"\n"typedeflonglongLL;constintN=1e6+10,M=N,mod=1e9+7;usingnamespacestd;inta[N],b[N],n,q;LLt[N];intlowbi......
  • 区间修改+区间查询的树状数组
    /*https://www.acwing.com/solution/content/44886/看acwing*/#include<bits/stdc++.h>#defineCLOSEios::sync_with_stdio(false);cin.tie(0);cout.tie(0)#defineendl"\n"typedeflonglongLL;constintN=1e6+10,M=N,mod=1e9+7;u......
  • java直连mysql操作数据
    连接器importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;/***@author:chenKeFeng*@date:2024/1/3010:21*/publicclassMySQLConnector{p......