js内置对象之Array面试
let word = ['a', 'b', 'c', 'd']; let newArr = word.pop(); console.log(word); //['a', 'b', 'c'] console.log(newArr); //d let nullArr = []; console.log(nullArr.pop()); //undefined
let word = ['a', 'b', 'c', 'd']; let newArr = word.push('e','f'); console.log(word); //['a', 'b', 'c', 'd', 'e', 'f'] console.log(newArr); //6
let word = ['a', 'b', 'c', 'd']; let newArr = word.shift(); console.log(word); //['b', 'c', 'd'] console.log(newArr); //a
let word = ['a', 'b', 'c', 'd']; let newArr = word.unshift('11','22'); console.log(word); //['11', '22', 'a', 'b', 'c', 'd'] console.log(newArr); //6
let fruit = ['cherries', 'apples', 'bananas']; console.log(fruit.sort()); // ['apples', 'bananas', 'cherries'] let scores = [1, 10, 21, 2]; console.log(scores.sort()); // [1, 10, 2, 21] // 注意10在2以前, // 由于在 Unicode 指针顺序中"10"在"2"以前 let things = ['word', 'Word', '1 Word', '2 Words']; console.log(things.sort()); // ['1 Word', '2 Words', 'Word', 'word'] // 在Unicode中, 数字在大写字母以前, // 大写字母在小写字母以前. function compare(a, b) { if(a < b) { return -1; }else if(a > b) { return 1; }else { return 0; } } let num = [1, 10, 21, 2]; console.log(num.sort(compare)); //[1, 2, 10, 21]
let word = ["a", "b", "c", "d"]; let newArr = word.reverse(); console.log(word); //["d", "c", "b", "a"] console.log(newArr); //["d", "c", "b", "a"]
var word = ['a', 'b', 'c', 'd']; //删除,前闭后开 var newArr = word.splice(0,2); console.log(word); //["c", "d"] console.log(newArr); //["a", "b"] //插入,当前数组索引1处插入hello var newArr = word.splice(1,0,'hello'); console.log(word); //["c", "hello", "d"] console.log(newArr); //[] //替换 var newArr = word.splice(1,1,'world'); console.log(word); //["c", "world", "d"] console.log(newArr); //["hello"]
let word = ['a', 'b', 'c', 'd']; let word2 = ['hello','world']; let newArr = word.concat(word2); console.log(word); //["a", "b", "c", "d"] console.log(newArr); //["a", "b", "c", "d", "hello", "world"]
let word = ['a', 'b', 'c', 'd']; let newArr = word.join('---'); console.log(word); //["a", "b", "c", "d"] console.log(newArr); //a---b---c---d
let word = ['a', 'b', 'c', 'd']; //原数组索引为1开始截取后面全部元素 let newArr = word.slice(1); console.log(word); //["a", "b", "c", "d"] console.log(newArr); //["b", "c", "d"] //截取原数组索引为1到3之间的元素,前闭后开 let newArr2 = word.slice(1,3); console.log(word); //["a", "b", "c", "d"] console.log(newArr2); //["b", "c"] //截取原数组倒数第三个元素与倒数第一个元素之间的元素,前闭后开 let newArr3 = word.slice(-3,-1); console.log(word); //["a", "b", "c", "d"] console.log(newArr3); //[["b", "c"]
let word = ['a', 'b', 'b', 'c', 'd']; let index = word.indexOf('b'); //1,第一次出现b的索引值 let index2 = word.indexOf('hello'); //-1 console.log(index); console.log(index2);
let word = ['a', 'b', 'b', 'c', 'd']; let index = word.lastIndexOf('b'); //2,最后一个b的索引值为2 let index2 = word.lastIndexOf('hello'); //-1 console.log(index); console.log(index2);
let word = ['a', 'b', 'b', 'c', 'd']; let str = word.toString(); //a,b,b,c,d console.log(str);
每一个方法接受含有三个参数的函数,三个参数为:数组中的项,元素索引,数组自己数组
1.every(),数组全部元素都知足要求则返回true,不然返回false
2.some(),只要有知足要求的就返回true
3.filter(),返回过滤后的结果数组
4.map(),返回在函数中处理过的数组
5.forEach(),遍历整个数组app
var number = [1,2,3,4,5,6,7,8]; var res = number.every(function(item, index, array) { return (item > 2); }) console.log(res); //false var res = number.some(function(item, index, array) { return (item > 2); }) console.log(res); //true var res = number.filter(function(item, index, array) { return (item > 2); }) console.log(res); //[3, 4, 5, 6, 7, 8] var res = number.map(function(item, index, array) { return (item * 2); }) console.log(res); //[2, 4, 6, 8, 10, 12, 14, 16] var res = number.forEach(function(item, index, array) { //执行某些操做 })
迭代数组全部项,构建最终返回值,每一个方法接受两个参数:调用的函数和做为归并基础的初始值。函数接受4个参数:前一个值,当前值,项索引,数组自己。函数返回的值都会做为第一个参数自动传给下一项,第一次迭代从数组第二项开始,当前值为数组第二项函数
1.reduce(),从数组第一项开始遍历到最后
2.reduceRight(),从数组最后一项开始遍历到第一项ui
/* 开始执行回调函数cur为2,prev为1, 第二次执行回调函数,在以前的基础上加1 函数返回的值都会做为一个参数传给下一项, 最后执行函数时就是28+8 */ var number = [1,2,3,4,5,6,7,8]; var res = number.reduce(function(prev, cur, index, array) { return prev + cur; }) console.log(res); //1+2+3+4+5+6+7+8=36 var res = number.reduceRight(function(prev, cur, index, array) { return prev + cur; }) console.log(res); //8+7+6+5+4+3+2+1=36
数组是除了函数对象以外在js中使用最多的数据类型,掌握一些数组中经常使用方法在使用js作开发时仍是会有帮助的,并且有些面试中也会问到相关问题,好比数组操做方法中哪些会改变原数组,哪些不会。更多数组方法请看MDN指针