当前位置:网站首页>JS数组常用方法
JS数组常用方法
2022-07-17 00:06:00 【静止呀】
1.push方法 可以一次加多个元素
this.letters.push('aaa')
2.通过索引值修改数组中的元素,下面写法无法做到响应式改变数据。
this.letters[0] = 'bbbbb'
console.log(this.letters)
3.pop() 从末尾删除掉
this.letters.pop(); // 响应式的
4.shift() 从头部删除掉
this.letters.shift(); // 响应式的
5.unshift() 在数组最前面添加元素 可以一次加多个元素
this.letters.unshift("abc","f");
6.splice() 可删除元素,添加元素,替换元素 splice(start,)
6.1.删除元素,第二个参数传入你要删除几个元素。如果第二个元素不传,则将start往后的所有元素删除
6.2.替换元素,第二个参数表示要替换几个元素,后面是用于替换前面的元素。
this.letters.splice(1,3,'m','n','l') // 从1开始替换三个(不包含1)
6.3.添加元素,第二个参数传入0,后面跟上要插入的参数。
从第二个位置开始,不删除参数,添加”你好“
this.letters.splice(2,0,"你好");
7.slice() 方法可从已有的数组中返回选定的元素。
// arrayObject.slice(start,end) -1最后一个元素。 end截取时不被包括。
arr = [1, 2, 3, 4, 5, 6, 7]
console.log( arr.slice(0) ) // 全部
console.log( arr.slice(2) ) // 3,4,5,6,7
console.log( arr.slice(1,2) ) // 2
console.log( arr.slice(1,3) ) // 2,3
console.log( arr.slice(0,-2) ) // 1,2,3,4,5
8.sort() 用于对数组的元素进行排序。
this.letters.sort();
var fruits = ["Banana", "Orange", "Apple", "Mango"];
输出Apple,Banana,Mango,Orange
// 升序
var points = [40,100,1,5,25,10];
points.sort(function(a,b){
return a-b});
// 降序
var points = [40,100,1,5,25,10];
points.sort(function(a,b){
return b-a});
9.reverse() 将数组内容进行反转
this.letters.reverse();
10.修改数组中的元素,Vue自带一个方法
// set(要修改的对象,索引值,修改后的值)
Vue.set(this.letters,0,'bull') // 修改索引0的数据为bull
边栏推荐
- 服务器如何安装宝塔面板?(宝塔面板安装教程)
- 量化行业知识汇总
- P6-day01-general attempt (P4 review)
- Stack injection [strong net cup 2019] random note
- Use leaflet to copy the original shentiwa Mega map to make a diary
- The C Programming Language (2nd)--笔记--1.6
- How to modify the color of a line of text in the elemtnui table (elemtnui table modifies the color of a line of text)
- XXX packages are looking for funding run `npm fund` for details解决方法
- Day13 mixed view base class
- promise
猜你喜欢

Summary of XML external entity injection (xxE target recurrence)

Uni app wechat applet - Mall (6) - my home page

Use leaflet to copy the original shentiwa Mega map to make a diary

elemtnui 表格如何修改某行文字颜色(elemtnui table 修改某行文字颜色)

Registry hijacking triggers malicious programs

The C Programming Language (2nd)--笔记--1.6

uni-app微信公众号(5)——新增、修改地址

Uni app wechat applet - Mall (7) - Product Details

Codeforces round #664C

Self encapsulated stylized switch card assembly
随机推荐
替换URL中的特殊字符(%E2%80%8B)
Raw Socket抓包,某些端口的数据包抓不到
Initial flask
elemtnui 表格如何修改某行文字颜色(elemtnui table 修改某行文字颜色)
Common mode attack and shared prime of RSA
Single page application spa and multi page application MPa
Day11 serializer
jsx 编译
Stack injection [strong net cup 2019] random note
【专题】用ST表解决RMQ刷题总结
[elementui El date picker date selector, the end time must not be earlier than the start time, and only the date of the specified number of days from the start time can be selected]
Codeforces round #664C
小程序嵌入网页向小程序跳转并传参,微信小程序中实现公众号授权获取openId
Express中间件的分类及使用
el-form特殊字符校验
About foreign key references, cross domain headers, and ref usage
uni-app微信公众号(1)——网页授权登录
Day14 view set and route
MoveIt2——4.机器人模型和机器人状态
tp-watermark.js网页添加水印插件