var arrNum = [1,2,3];
var arrColor = ['red','yellow','blue'];
var arr = [1,3,'red','blue',5,{name:'qqq946'}];
// 所有对象都具有 toLocaleString()、toString()、valueOf()方法。
// 1. 数组的每一项可以保存任何类型的数据。
// 2. 数组的 length 属性不是只读的。
// 一、转换方法
// 1. toString()
// 返回一个字符串,该字符串由数组中每个值的字符串形式拼接而成,以逗号分隔。
console.log(arr.toString()); // 1,3,red,blue,5,[object Object]
// 2. valueOf()
// 返回一个数组
console.log(arr.valueOf()); // Array(6) [ 1, 3, "red", "blue", 5, {…} ]
// 二、栈方法
// 1. push()
// 接收任意数量参数,把他们添加到数组末尾,返回新数组长度。
console.log(arrNum.push(4,5)); // 5
// 2. pop()
// 移除数组末尾最后一项,返回移除的项。
console.log(arrNum.pop()); // 5
console.log(arrNum.pop()); // 4
// 三、队列方法
// 1. unshift()
// 在数组前端添加任意项,并返回新数组长度
console.log(arrNum.unshift(0));
// 2. shift()
// 移除数组第一项,并返回该项。
console.log(arrNum.shift());
// 四、重排序方法
// 1. reverse()
console.log(arrNum.reverse());
console.log(arrNum.reverse());
// 2. sort()
// 五、操作方法
// 1. concat()
// 创建当前数组的副本,有参数的话添加到新数组中。返回新数组。
console.log(arr.concat(111,[222,555],666)); // Array(10) [ 1, 3, "red", "blue", 5, {…}, 111, 222, 555, 666 ]
// 2. slice()
// 接受一个或两个参数,即要返回项的起始和结束位置。
// 一个参数,返回该参数指定位置到数组末尾所有项。
// 两个参数,返回起始和结束位置之间的项,但不包括结束位置。
// 该方法不会影响原始数组。
console.log(arrNum.slice(0,2)); Array [ 1, 2 ]
// 3. splice()
// 参数:起始位置,删除项数,插入项/替换项
// 返回一个数组,该数组中包含删除项,没有删除项则返回空数组。
console.log(arrNum.splice(0,0,'a','b'));
console.log(arrNum.splice(0,2));
// 六、位置方法
// 1. indexOf()
// 参数:要查找的项,查找起点(可选)
// 从数组开头查找,返回要查找的项在数组中的位置,没找到返回-1。
// 比较时会使用全等操作符===。
console.log(arrNum.indexOf(3)); // 2
// 2. lastIndexOf()
// 倒序查找
console.log(arrNum.lastIndexOf(1)); // 0
// 七、迭代方法
// 每个方法接收两个参数:要在每一项上运行的函数,运行该函数的作用域对象——影响this(可选)。
// 传入这些方法中的函数会接收三个参数:数组项的值、该项在数组中的位置、数组对象本身。
// 1. every()
// 对数组中每一项运行给定函数。每一项都返回 true,则返回 true。
// 2. filter()
// 对数组中每一项运行给定函数。返回该函数会返回 true 的项组成的数组。
// 3. forEach()
// 对数组中每一项运行给定函数。没有返回值。本质上与使用 for 循环迭代数组一样。
// 4. map()
// 对数组中每一项运行给定函数。返回每次函数调用的结果组成的数组。
// 5. some()
// 对数组中每一项运行给定函数。如果该函数任意一项返回 true,则返回 true。
console.log(arr.every(function(value){
return value>5
}))
console.log(arr.filter(function(item){
return item<5
}))
console.log()
arr.forEach(function(item){
console.log(item)
})
// 八、缩小方法
// 两个参数:每一项上调用的函数,初始值(可选)。
// 传入的函数可接收四个参数:前一个值、当前值、项的索引、数组对象。
// 返回最后的值。
// 1. reduce()
// 2. reduceRight()