js数组push对象,js数组push改变原数组吗

http://www.itjxue.com  2023-01-18 01:40  来源:未知  点击次数: 

js数组对象常用的方法

数组转换字符串

array.join()? ? // 将数组的值拼接成字符串。

var arr = [1,2,3,4,5]

arr.join()?? // 不传参数,默认按【,】进行拼接

arr.join("-")? // 参数是字符串类型 按【-】进行拼接

数组的增删操作

array.push(value,[value[,...]])?? //将一个或多个元素添加到数组的结尾,并返回新的长度。

array.pop()?? //从数组中删除最后一个元素,并返回该元素的值,改变了数组的长度

array.unshift(value,[value[,...]])?? //将一个或多个元素添加到数组的开头,并返回新的长度。

array.shift()? ? //从数组中删除第一个元素,并返回该元素的值,改变了数组的长度

数组的翻转与排序

array.reverse()? ? // 翻转数组,返回翻转过的数组

array.sort()? ? // 默认排序顺序是根据字符串Unicode码点,比如如果是数字的话,排序结果是10小于2,因为比较的是第一位

var arr = [1,3,10,2,4,40,5]

arr.sort()? ? // [1, 10, 2, 3, 4, 40, 5]

// sort方法可以传递一个函数作为参数,这个参数用来控制数组如何进行排序

var arr = [1,2,10,4,40,5]

arr.sort(function(a,b)

? ? return a-b;? //按照正序

? ? return b-a //按照倒序

})

数组的拼接与截取

concat:数组合并,不会影响原来的数组,会返回一个新数组。

var arr = [1,2,3]

var arr1 = ["a","b","c"]

var newArray = arr.concat(arr1)? ? //[1,2,3,"a","b","c"];

slice:复制数组的一部分到一个新数组,并返回这个新数组,原来的数组不受影响,包含头,不包含尾

var newArray = array.slice(begin, end)

var arr = [1,2,3,4,5]

arr.slice(0,3) //[1,2,3]

arr.slice(-3,-1) //[3,4]

splice: 以新元素来替换旧元素,以此来修改数组的内容,返回被替换的内容,原数组被改变

start:开始位置? deleteCount:删除的个数? items:替换的内容

array.splice(start, deleteCount, [items[,items...])

var arr = [1,2,3,4,5]

var newArray = arr.splice(0,3,"a","b","c","d")

console.log(newArray)? ? //[1, 2, 3]

console.log(arr)? ? // ["a", "b", "c", "d", 4, 5]

数组查找元素

indexOf方法用来查找数组中某个元素第一次出现的位置,如果找不到,返回-1

array.indexOf(search, [fromIndex])

lastIndexOf()从后面开始查找数组中元素第一次出现位置,如果找不到,返回-1?

array.lastIndexOf(search, [fromIndex])

链接:

JS数组对象中有相同值的数据拿出相同的对象重组

const List = [{"end_date":"2022-02-10","end_seconds":1644488295.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-10 18:08:34","id":10319,"last_heart_beat_time":"2022-02-10 18:18:15","scanner_id":167,"start_date":"2022-02-10","start_seconds":1644487714.0,"start_total_seconds":581},{"end_date":"2022-02-11","end_seconds":1644550497.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-11 11:19:08","id":10338,"last_heart_beat_time":"2022-02-11 11:34:57","scanner_id":167,"start_date":"2022-02-11","start_seconds":1644549548.0,"start_total_seconds":949},{"end_date":"2022-02-11","end_seconds":1644558220.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-11 11:48:41","id":10339,"last_heart_beat_time":"2022-02-11 13:43:40","scanner_id":167,"start_date":"2022-02-11","start_seconds":1644551321.0,"start_total_seconds":6899},{"end_date":"2022-02-11","end_seconds":1644558820.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-11 13:53:40","id":10343,"last_heart_beat_time":"2022-02-11 13:53:40","scanner_id":167,"start_date":"2022-02-11","start_seconds":1644558820.0,"start_total_seconds":0},{"end_date":"2022-02-11","end_seconds":1644563620.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-11 14:03:41","id":10344,"last_heart_beat_time":"2022-02-11 15:13:40","scanner_id":167,"start_date":"2022-02-11","start_seconds":1644559421.0,"start_total_seconds":4199},{"end_date":"2022-02-11","end_seconds":1644586551.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-11 15:55:51","id":10350,"last_heart_beat_time":"2022-02-11 21:35:51","scanner_id":167,"start_date":"2022-02-11","start_seconds":1644566151.0,"start_total_seconds":20400},{"end_date":"2022-02-13","end_seconds":1644759468.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-13 14:47:48","id":10410,"last_heart_beat_time":"2022-02-13 21:37:48","scanner_id":167,"start_date":"2022-02-13","start_seconds":1644734868.0,"start_total_seconds":24600},{"end_date":"2022-02-14","end_seconds":1644846304.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-14 10:15:05","id":10430,"last_heart_beat_time":"2022-02-14 21:45:04","scanner_id":167,"start_date":"2022-02-14","start_seconds":1644804905.0,"start_total_seconds":41399},{"end_date":"2022-02-15","end_seconds":1644911999.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-15 15:30:00","id":10468,"last_heart_beat_time":"2022-02-15 15:59:59","scanner_id":167,"start_date":"2022-02-15","start_seconds":1644910200.0,"start_total_seconds":1799},{"end_date":"2022-02-15","end_seconds":1644932084.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-15 19:44:44","id":10472,"last_heart_beat_time":"2022-02-15 21:34:44","scanner_id":167,"start_date":"2022-02-15","start_seconds":1644925484.0,"start_total_seconds":6600},{"end_date":"2022-02-16","end_seconds":1645020217.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-16 15:13:37","id":10491,"last_heart_beat_time":"2022-02-16 22:03:37","scanner_id":167,"start_date":"2022-02-16","start_seconds":1644995617.0,"start_total_seconds":24600},{"end_date":"2022-02-17","end_seconds":1645089245.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-17 14:59:06","id":10521,"last_heart_beat_time":"2022-02-17 17:14:05","scanner_id":167,"start_date":"2022-02-17","start_seconds":1645081146.0,"start_total_seconds":8099}]

? ? ? ? const newArray = []

? ? ? ? List.forEach(item = {

? ? ? ? ? ? // 第一次循环,repeat定义repet,基于find浅拷贝特性,操作repeat会影响到newArray

? ? ? ? ? ? // 循环开始,find的条件不成立,就会push一个新对象{data: item.start_date,times: [item]}进去

? ? ? ? ? ? // 检索条件要注意,find的核对条件需要准确

? ? ? ? ? ? // 第二次循环,find条件成立,则走else

? ? ? ? ? ? const repeat = newArray.find(res = res.data === item.start_date)

? ? ? ? ? ? if (!repeat) {

? ? ? ? ? ? ? ? newArray.push({

? ? ? ? ? ? ? ? ? ? data: item.start_date,

? ? ? ? ? ? ? ? ? ? times: [item]

? ? ? ? ? ? ? ? })

? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? // 避免循环紊乱,不用item.times.push(item),选用repeat.times.push(item)

? ? ? ? ? ? ? ? repeat.times.push(item)

? ? ? ? ? ? }

? ? ? ? })

打印数据后可以看到,已经将数据成功照相同时间的start_date重新管理

如果前端需要解决时间归类当天的用户多部操作但是后端没处理,就可以用forEach+find的方法处理

JS 数组push对象 如何打断和原对象的联系

因为a[0]是个对象,所以为引用类型;引用类型栈内存中存放的是指向堆内存中的指针,也就是说push后b中存放的是能访问a[0]堆内存的指针,则b[0]的指针所指向的值与a[0]指针指向的值是同一个,内存也是同一个;此时,你再对b[0]中的c属性进行操作,则改内存中的值改变,则a[0]的值同样改变,这里就是引用类型的基本存储问题。

要解决的话可采用深拷贝的方法,将a[0]的指针与b[0]的指针分别指向不同的内存即可解决

可将第三行修改为b.push(JSON.parse(JSON.stringify(a[0])))

js数组对象操作

声明创建一个数组对象: var arr = new Array(); 或者 var arr = [];

连接两个或者多个数组,并且返回该数组,语法: array.concat(object,object,......);

通过指定字符(参数)对数据进行分割,返回字符串,参数省略的话则用默认用逗号为分隔符

删除数组的最后一个对象,返回该删除元素的值

向数组末尾添加一个或者多个对象,语法: array.push(newObject1,newObject2,.....);

删除数组的第一个对象,并返回删除的元素

向数组开头添加一个或者多个元素,并返回新的长度

从已知数组中返回指定选中的数据(不包括end 对应的元素),如果省略 end 将复制 start 之后的所有元素,该操作不会修改原数组的数据, slice(start,end);

向数组中删除/添加对象,并返回被删除的元素

splice(index,count,item1,item2,......);

方法用户对数组的排序, sort(sortby) ,sortby可选,必须是函数。如调用方法没有使用参数,则按字母顺序进行排序。

只要有一个满足的就返回true,没有满足的返回false

验证数组中是否每个元素都满足指定的条件

没有返回值,可以不知道数组长度

arr.forEach(function(res,index){ })

返回值组成新数组,原数组不变

过滤通过条件的元素组成一个新数组,原数组不变

查找出第一个符合条件的数组成员,并返回该成员,如果没有找到就返回undefine

找到的是位置,找不到返回 -1

填充, arr.fill(填充的东西,start,end) 包括end

(责任编辑:IT教学网)

更多