标签:操作 empty value pop 布尔值 表达 spl 转换 下标
我们平常在书写代码的时候遇到需要使用多个数据,通过定义多个变量太麻烦了,所以就定义了数组。
数组是一组数据的集合,用来存放多个数据。适用于需要操控多个数据,但是只能使用一个数据的情况。
var arr = [5]; //[5]代表数组里只有一个成员5
var arr1 = new Array(5);//[empty × 5] 代表数组长度为5,成员为五个空
console.log(arr);
console.log(arr1);
var arr = [1, 2, 3, 4];
var length = arr.push(6);
console.log(length);//5
console.log(arr);//[1, 2, 3, 4, 6];
var arr = [1, 2, 3, 4];
var length = arr.unshift(6);
console.log(length);//5
console.log(arr);//[6, 1, 2, 3, 4];
var arr = [1, 2, 3, 4];
var num = arr.pop();
console.log(num);//4
console.log(arr);//[1, 2, 3,];
var arr = [1, 2, 3, 4];
var num = arr.shift();
console.log(num);//1
console.log(arr);//[2, 3, 4];
var arr = [3, 7, 9, 8, 1, 6];
arr.sort(function(a,b){
return a - b;
});//[1, 3, 6, 7, 8, 9]
console.log(arr);
var arr = [3, 7, 9, 8, 1, 6];
arr.sort(function(a,b){
return b - a;
});//[9, 8, 7, 6, 3, 1]
console.log(arr);
var arr = [3, 7, 9, 8, 1, 6];
arr.sort(function(){
});//[3, 7, 9, 8, 1, 6]
console.log(arr);
var arr = [1, 2, 3, 4, 5, 6];
var str = arr.join();
var str1 = arr.join("*");
console.log(str);//1,2,3,4,5,6
console.log(str1);//1*2*3*4*5*6
var arr = [1, 2, 3, 4, 5, 6];
var arr1 = arr.concat(7,8,9,[10,11,23]);
console.log(arr1);//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 23]
var arr = [1, 2, 3, 4, 5, 6];
var arr1 = arr.slice();
console.log(arr1);//[1, 2, 3, 4, 5, 6]
var arr = [1, 2, 3, 4, 5, 6];
var arr1 = arr.slice(2);
console.log(arr1);//[3, 4, 5, 6]
var arr = [1, 2, 3, 4, 5, 6];
var arr1 = arr.slice(2,4);
console.log(arr1);//[3, 4]
var arr = [1, 2, 3, 4, 5, 6];
var arr1 = arr.slice(3,-2);
console.log(arr1);//[4]
var arr = [1, 2, 3, 4, 5, 6];
arr.reverse();
console.log(arr);//[6, 5, 4, 3, 2, 1]
var arr = [1, 2, 3, 4, 5, 6];
arr.splice(1,2,7,8,9);
console.log(arr);//[1, 7, 8, 9, 4, 5, 6]
var arr = [1, 2, 3, 4, 5, 6, 7, 8];
var index = arr.indexOf(5,4);
console.log(index);//4
var arr = [1, 2, 3, 4, 5, 6, 7, 8];
var arr1 = arr.filter(function(value,index,self){
return value % 2 == 1;
})
console.log(arr1);//[1, 3, 5, 7]
var arr = [1, 2, 3, 4, 5, 6, 7, 8];
arr.forEach(function(value,index,self){
console.log(value,index,self);
})
var arr = [10, 20, 30, 40, 50, 60, 70, 80];
var arr1 = arr.map(function(value,index,self){
return value * 1.5
})
console.log(arr1);//[15, 30, 45, 60, 75, 90, 105, 120]
var arr = [1, 2, 3, 4, 5, 6, 7, 8];
var result = arr.some(function(value,index,self){
return value === 5;
})
console.log(result)//true
var arr = [1, 2, 3, 4, 5, 6, 7, 8];
var result = arr.every(function(value,index,self){
return value === 5;
})
console.log(result)//false
var arr = [1, 2, 3, 4, 5, 6, 7, 8];
arr.fill(9);
console.log(arr);//[9, 9, 9, 9, 9, 9, 9, 9]
var arr = [1, 2, 3, 4, 5, 6, 7, 8];
arr.fill(9,3);
console.log(arr);//[1, 2, 3, 9, 9, 9, 9, 9]
var arr = [1, 2, 3, 4, 5, 6, 7, 8];
arr.fill(9,3,6);
console.log(arr);//[1, 2, 3, 9, 9, 9, 7, 8]
var arr = ["张三", "李四", "王五", "赵六", "田七", "刘八"];
var username = arr.find(function(value,index,self){
return value.indexOf("四") != -1;
})
console.log(username);//李四
var arr = ["张三", "李四", "王五", "赵六", "田七", "刘八"];
var Index = arr.findIndex(function(value,index,self){
return value.indexOf("四") != -1;
})
console.log(Index);//1
var arr = [1, 2, 3, 4, 5, 6, 7, 8];
arr.copyWithin(1,3,7);
console.log(arr);//?[1, 4, 5, 6, 7, 6, 7, 8]
标签:操作 empty value pop 布尔值 表达 spl 转换 下标
原文地址:https://www.cnblogs.com/tongmeng/p/11610023.html