1、数组中有另一个数组,访问里边数组的数据
let age = [1,2,3,[1,2,3]] age[3][1] //第一个[]是里边的数组的下标,第二个[]是数组中的数据 //2
2、字符串变成数组splict
let name = 'the,first,item,in,the,array' name.splict(",") //通过逗号分隔字符串,变成一个新的数组
3、把数组变成字符串jion()
let age = [1,2,3,'3',1,'2'] age.jion(",")//通过逗号使数组变成字符串
tostring()方法
let name = ['zhang','shuang'] name.tostring() //通过tostring方法使数组变成字符串
4、再数组末尾增加一个项目push()
let myArray = ['Manchester', 'London', 'Liverpool', 'Birmingham', 'Leeds', 'Carlisle']; myArray.push("zhang")
5、删除数组中的最后一个数据pop()
let myArray = ['Manchester', 'London', 'Liverpool', 'Birmingham', 'Leeds', 'Carlisle']; myArray.pop()
6、再数组的第一位增加数据unshift()
let myArray = ['Manchester', 'London', 'Liverpool', 'Birmingham', 'Leeds', 'Carlisle']; myArray.unshift('Edinburgh');
7、删除数组中的第一个数据shift()
let myArray = ['Manchester', 'London', 'Liverpool', 'Birmingham', 'Leeds', 'Carlisle']; myArray.shift();
8、数组过滤filter
let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; let res = nums.filter((num) => { return num > 5; }); console.log(res); // [6, 7, 8, 9, 10]
过滤数组
const arr = [ { name: 'tom1', age: 23 }, { name: 'tom2', age: 42 }, { name: 'tom3', age: 17 }, { name: 'tom4', age: 13 }, ] const res = arr.filter(item => item.age > 18); console.log(res); //[{name: 'tom1',age: 23},{name: 'tom2',age: 42}] console.log(arr);
9、数组的拼接concat
var sedan = ["S60", "S90"]; var SUV = ["XC40", "XC60", "XC90"]; var Volvo = sedan.concat(SUV); 结果:"S60", "S90","XC40", "XC60", "XC90"
10、包含includes
arr.includes(6) 数组arr是否包含6