js操作数组的方法
一、检测数组
1、value instanceof Array
2、Array.isArray(value)
二、转换方法
1、toString() :数组转换为字符串,已 ',' 分割字符串
2、valueOf() : 返回 Array 对象的原始值
3、toLocaleString() :数组转换为字符串,调用每个数组元素的 toLocaleString() 方法,然后使用地区特定的分隔符把生成的字符串连接起来,形成一个字符串。
4、join(separator) :数组转换为字符串,已参数separator字符串分割字符串
var colors = ["red", "green", "blue"]; console.log(colors.toString()) // red,green,blue console.log(colors.toLocaleString()) // red,green,blue console.log(colors.join('|')) // red|green|blue console.log(colors.valueOf()) // ["red", "green", "blue"]
三、栈方法
栈是一种 LIFO(Last-In-First-Out,后进先出)的数据结构,也就是最新添加的项最早被移除。
1、push() 方法可以接收任意数量的参数,把它们逐个添加到数组末尾,并返回修改后数组的长度
2、pop() 方法则从数组末尾移除最后一项,减少数组的 length 值,然后返回移除的项
var colors = ["red", "green", "blue"]; var count = colors.push("red11", "green11"); // 推入两项 console.log(count); // 5 console.log(colors) // ["red", "green", "blue", "red11", "green11"] var item = colors.pop(); // 取得最后一项 console.log(item); // "green11" console.log(colors.length); //4
四、队列方法
队列数据结构的访问规则是 FIFO(First-In-First-Out,先进先出)
1、shift() ,它能够移除数组中的第一个项并返回该项,同时将数组长度减 1
2、unshift() ,它能在数组前端添加任意个项并返回新数组的长度
结合使用 shift() 和 push() 方法,可以像使用队列一样使用数组
var colors = ["red", "green", "blue"]; var count = colors.push("red11", "green11"); // 推入两项 console.log(count); // 5 console.log(colors) // ["red", "green", "blue", "red11", "green11"] var item = colors.shift(); // 取得第一项 console.log(item); // "red" console.log(colors); // ["green", "blue", "red11", "green11"]
使用 unshift() 和 pop() 方法,可以从相反的方向来模拟队列,即在数组的前端添加项,从数组末端移除项
var colors = ["red", "green", "blue"]; var count = colors.unshift("red11", "green11"); // 推入两项 console.log(count); // 5 console.log(colors) // ["red11", "green11", "red", "green", "blue"] var item = colors.pop(); // 取得第一项 console.log(item); // "blue" console.log(colors); // ["red11", "green11", "red", "green"]
五、重排序方法
1、 sort() 方法 : 会调用每个数组项的 toString() 转型方法,然后比较得到的字符串,以确定如何排序。在默认情况下, sort() 方法按升序排列数组项——即最小的值位于最前面,最大的值排在最后面。
var values = [0, 1, 5, 10, 15];
values.sort();
alert(values); //0,1,10,15,5
sort() 方法可以接收一个比较函数作为参数,以便我们指定哪个值位于哪个值的前面。
function compare(value1, value2) { if (value1 < value2) { return -1; } else if (value1 > value2) { return 1; } else { return 0; } } var values = [0, 1, 5, 10, 15]; values.sort(compare); alert(values); //0,1,5,10,15
2、 reverse() 方法:反转数组原来的顺序
var colors = ["5", "8", "6rrrr", "6"];
console.log(colors.reverse()) // ["6", "6rrrr", "8", "5"]
对于数值类型或者其 valueOf() 方法会返回数值类型的对象类型,可以使用一个更简单的比较函数。这个函数只要用第二个值减第一个值即可。由于比较函数通过返回一个小于零、等于零或大于零的值来影响排序结果,因此减法操作就可以适当地处理所有这些情况。
function compare(value1, value2){ return value2 - value1; }
六、操作方法
1、 concat() 方法 :可以基于当前数组中的所有项创建一个新数组。具体来说,这个方法会先创建当前数组一个副本,然后将接收到的参数添加到这个副本的末尾,最后返回新构建的数组。在没有给 concat() 方法传递参数的情况下,它只是复制当前数组并返回副本。如果传递给 concat() 方法的是一或多个数组,则该方法会将这些数组中的每一项都添加到结果数组中。如果传递的值不是数组,这些值就会被简单地添加到结果数组的末尾。
var colors = ["red", "green", "blue"]; var colors2 = colors.concat(); var colors3 = colors.concat("yellow", ["black", "brown"]);
var colors4 = colors.concat("yellow", ["black", ["black11", "brown111"]]); console.log(colors2); // ["red", "green", "blue"] console.log(colors3); // ["red", "green", "blue", "yellow", "black", "brown"]
console.log(colors3); // ["red", "green", "blue", "yellow", "black", ["black11", "brown111"]]
2、slice() :可以接受一或两个参数,即要返回项的起始和结束位置。
在只有一个参数的情况下, slice() 方法返回从该参数指定位置开始到当前数组末尾的所有项。
如果有两个参数,该方法返回起始和结束位置之间的项——但不包括结束位置的项。
如果 slice() 方法的参数中有一个负数,则用数组长度加上该数来确定相应的位置。如果结束位置小于起始位置,则返回空数组。
注意, slice() 方法不会影响原始数组。
var colors = ["red", "green", "blue", "yellow", "purple"]; var colors2 = colors.slice(1); var colors3 = colors.slice(1,4); console.log(colors2); // ["green", "blue", "yellow", "purple"] console.log(colors3); // ["green", "blue", "yellow"]
3、splice() 方法 :向数组的中部插入项
1)删除:可以删除任意数量的项,只需指定 2 个参数:要删除的第一项的位置和要删除的项数。
var colors = ["red", "green", "blue", "yellow", "purple"]; var remove = colors.splice(2) // 只有一个起始位置参数,默认为删除从起始到结尾的 console.log(remove); // ["blue", "yellow", "purple"] console.log(colors); // ["red", "green"] var colors = ["red", "green", "blue", "yellow", "purple"]; var remove02 = colors.splice(-2) // 若只有一个负数,则从数组末尾开始删除到参数绝对值个为止 console.log(remove02); // ["yellow", "purple"] console.log(colors); // ["red", "green", "blue"]
var colors = ["red", "green", "blue", "yellow", "purple"]; var remove03 = colors.splice(2, 1) // 从位置2开始删除1个 console.log(remove03); // ["blue"] console.log(colors); // ["red", "green", "yellow", "purple"] var colors = ["red", "green", "blue", "yellow", "purple"]; var remove04 = colors.splice(-2, 1) // 从倒数位置-2开始删除1个 console.log(remove04); // ["yellow"] console.log(colors); // ["red", "green", "blue", "purple"]
2)插入:可以向指定位置插入任意数量的项,只需提供 3 个参数:起始位置、0(要删除的项数)和要插入的项。如果要插入多个项,可以再传入第四、第五,以至任意多个项。
var colors = ["red", "green", "blue"]; removed = colors.splice(1, 0, "yellow333", "orange333"); // console.log(removed); // [] console.log(colors); // ["red", "yellow333", "orange333", "green", "blue"]
3)替换:可以向指定位置插入任意数量的项,且同时删除任意数量的项,只需指定 3 个参数:起始位置、要删除的项数和要插入的任意数量的项。插入的项数不必与删除的项数相等。
var colors = ["red", "green", "blue"]; removed = colors.splice(1, 1, "yellow333", "orange333"); // console.log(removed); // ["green"] console.log(colors); // ["red", "yellow333", "orange333", "blue"]
splice() 方法始终都会返回一个数组,该数组中包含从原始数组中删除的项(如果没有删除任何项,则返回一个空数组)。如果参数2大于0,原数组会改变。
七、位置方法
indexOf() 和 lastIndexOf() :这两个方法都接收两个参数:要查找的项和(可选的)表示查找起点位置的索引。
indexOf() 方法从数组的开头(位置 0)开始向后查找, lastIndexOf() 方法则从数组的末尾开始向前查找。
var numbers = [1,2,3,4,5,5,4,3,2,1,3,2,1]; console.log(numbers.indexOf(4)) // 3 console.log(numbers.lastIndexOf(4)) // 6 var person = { name: "Nicholas" }; var people = [{ name: "Nicholas" }]; var morePeople = [person]; alert(people.indexOf(person)); // -1 alert(morePeople.indexOf(person)); // 0
js操作数组的方法 02:https://www.cnblogs.com/xingyue525/p/13932082.html