标签:没有 key index start 结束 遍历数组 art 插入 OLE
书写格式:数组名.push(数据);
例:let arr = ["A","B","C"];
arr.push("D");
console.log(arr); //["A","B","C","D"]
书写格式:数组名.pop();
例:let arr = ["A","B","C"];
arr.pop();
console.log(arr); //["A","B"]
书写格式:数组名.unshift(数据);
例:let arr = ["A","B","C"];
arr.push(1);
console.log(arr); //[1,"A","B","C"]
书写格式:数组名.shift();
例:let arr = ["A","B","C"];
arr.shift();
console.log(arr); //["B","C"]
添加是可以,一次性添加多个数据;
删除每次只能删除一个;
例: let arr = [1,2];
let arr1 = [3,4];
let arr2 = arr.concat(arr1);
let arr3 = [...arr,...arr1];
console.log(arr2); //[1,2,3,4]
console.log(arr3); //[1,2,3,4]
书写格式:数组名.slice(参数); //可以接收两个参数
例:
let arr = ["A","B","C","D","E","F"];
let arr1 = arr.slice(2);
console.log(arr1); //["C","D","E","F"]
let arr2 = arr.slice(2,4);
console.log(arr2); //["C","D"]
let arr3 = arr.slice(10);
console.log(arr3); //[ ]
let arr4 = arr.slice(-4,-2);
console.log(arr3); //["C","D"]
书写格式:数组名.splice(参数); //可以接收三个参数
例:
let arr = ["A","B","C","D","E","F"];
let arr1 = arr.splice(2,5);
console.log(arr1); //["C","D","E","F"]
console.log(arr); //["A","F"]
let arr2 = arr.splice(1,0,5); //在下标为 1 的前面插入内容 5
console.log(arr2); //["A",5,"B","C","D","E","F"]
let arr3 = arr.splice(1,1,5); //将下标为 1 的数据改为内容 5
console.log(arr2); //["A",5,"C","D","E","F"]
//情况三: N个参数
从第三个参数开始,以后的内容都是要插入的值
let arr4 = arr.splice(1,0,5,6,7,8); //在下标为 1 的前面插入内容 5 6 7 8
console.log(arr4); //["A",5,6,7,8,"B","C","D","E","F"]
let arr = ["A","B","C","D","E","F"];
arr.length = 0;
console.log(arr); //[ ]
let arr = [1,2,3];
let str = arr.join(",");
console.log(str,typeof str); //1,2,3 string
let str = "Say hello to everybody!";
let arr = str.split();
console.log(arr); //[Say hello to everybody!
]
arr = str.split("");
console.log(arr); //[S``a``y`` ``h``e``l``l``o`` ``t``o`` ``e``v``e``r``y``b``o``d``y``!
]
arr = str.split(" ");
console.log(arr); //[‘Say‘,‘hello‘,‘to‘,‘everybody!‘]
let arr = ["A","B","C","D","E","F"];
for(let item of arr){
console.log(item);
}
//输出结果为 A B C D E F
for(let item in arr){
console.log(item);
}
//输出结果为:0 1 2 3 4 5
for(let item of arr.keys()){
console.log(item);
}
//输出结果为:0 1 2 3 4 5
for(let item of arr.values()){
console.log(item);
}
//输出结果为 A B C D E F
for(let item of arr.entries()){
console.log(item);
}
//输出结果为 [0,‘A‘] [1,‘B‘] [2,‘C‘] [3,‘D‘] [4,‘E‘] [5,‘F‘]
let str = "123";
let arr = ["A","B","C"];
console.log(Array.isArray(str)); //false
console.log(Array.isArray(arr)); //true
let arr = ["A","B","C","D","C"];
arr.includes("C"); //true
arr.indexOf("C"); //2
arr.lastIndexOf("C");//4
arr.indexOf("E");\arr.lastIndexOf(); //-1
通过下标或索引号获取到具体值。
数组名[下标]
注:下标(索引)的起始值是0
例:
let arr = ["A","B","C","D"];
console.log(arr); //输出结果为 ["A","B","C","D"]
console.log(arr[0]); //输出结果为 A
console.log(arr[2]); //输出结果为 C
console.log(arr[10]); //输出结果为 undefined
标签:没有 key index start 结束 遍历数组 art 插入 OLE
原文地址:https://www.cnblogs.com/hg845740143/p/12180234.html