Remember an interview question and common methods of js

interview questions

I remember a question from my previous interview: I don't remember the complete question, but I only remember the general requirements

  1. Basic question: write a method: pass parameter word, if pass parameter 3, generate a table similar to the following
1 1 1
2 2 2
3 3 3

If you pass parameter 4, the following table will be generated

1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4

and so on

My idea is:

  • numIn order to pass parameters, first initialize an array with a length of 1 num, and the array elements are filled with 1 ; initialize an array of a certain length: new Array(length), and the array is filled with values .fill();
  • Traverse the array, initialize each element as an array of length num, and fill the index value + 1 ; the maptraversal method is used here, which maprequiresreturn
function arr(num){
    return new Array(num).fill(1).map((item,i)=>{
        return new Array(num).fill(i+1);
   });
}
复制代码

The results are as follows:

image.png2. Extended question On the basis of 1, transform the content of the generated table as follows:

1 2 3
6 5 4
7 8 9
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13

By analogy my thinking is:

  • On the basis of 1, initialize numthe array of length, the array is filled with 1

  • mapmethod, re-initialize an array with a length of 1 for the elements numin the array, the default is 1 , and the first is preset 索引*num + 1. It should be noted here that a value must be preset for the array, otherwise, new Array(num)the resulting array is actually empty*numempty. The traverser cannot traverse to get the indexfollow item, as shown below

    image.png

  • Traverse the two-dimensional array again map, remove the first value, and add +1 to the remaining values

  • Judging odd-numbered and even-numbered lines, starting from 0, odd-numbered lines are required reverse(), and odd-even numbers pass the 对2取余judgment

  • return arry;Return the concatenated array

function arr(num){
    let array = new Array(num).fill(1).map((item,i)=>{
        let arrItem = new Array(num).fill(1).fill(i*num + 1, 0 ,1);
        let res = arrItem.map((val, key) => { return key === 0 ? val : key+arrItem[0];});
        return i%2 === 1 ? res.reverse() : res;
        
   }); 
   return array;
}
复制代码

The results are as follows:image.png

reflection

这次笔试题,写完后自己在console里面试了下发现有问题,forEachmap用错了,reduce方法也用错了~

基础还是不够扎实,于是趁这次机会要来巩固下数组字符串切割等的各种方法了~

map遍历

不会改变原来的数组,返回一个新数组

image.png

forEach遍历

用于对数组中元素进行操作,不改变原数组的值,返回值为undefined

image.png

filter过滤器 筛选数组

filter用来筛选符合条件的值,返回一个新的数组,不会对原来数组进行修改

image.png

reduce求数组总和

常见用法如下:

  1. 不额外传参:reduce((sum,item,index)=>{return sum+item}) 该方法表示从数组的第1个(即索引值index为1)开始循环,sum初始值为数组下标为0的值
  2. 传初始值:reduce((sum,item,index)=>{return sum+item}, initData) 该方法表示,从数组第0个(即索引值index为0)开始循环,sum初始值为initData

image.png

fill方法 给数组批量填充值

fill用于给数组初始化赋值,常见用法有以下

  1. array.fill(data): 只传一个参,表示给数组每个元素赋值data
  2. array.fill(data, startIndex, endIndex): 表示给数组索引值为startIndexendIndex之间的赋值data
  3. array.fill(data, startIndex): 表示给数组从startIndex索引值开始赋值data

image.png

splice方法

array.splice(index, howmany ,item1……itemX) 删除或新增元素,会修改到原有数组,若删除,返回删除元素组成的数组

  1. arr.splice(index, 0, item1……itemX) 表示从index索引后插入item……itemX数据

image.png

  1. arr.splice(index, howmany) 表示从index索引开始删除howmany个值,返回被删除元素组成的数组

image.png

  1. arr.splice(index, howmany, item1……itemX) 表示从index索引开始删除howmany个值,并插入item……itemX数据,返回被删除元素组成的数组

image.png

image.png

split方法

str.split(), 指定分隔符将字符串分割成数组,分隔符传参可为字符串或者正则

image.png

slice方法

slice(start[, end]), start end为正整数或负整数,用于切割字符串,不改变原有字符串

substring

substring(start[, end]), start end均为非负整数,用于切割字符串,不改变原有字符串

substr

substr(start[, length])start可为负数,表示倒数第几个,用于切割字符串,不改变原有字符串

总结

1、会改变原有数组或字符串的方法

  • splice:原有数组进行新增或删除操作,并且返回被删除元素组成的数组
  • fill: 原有数组预填值

2、不会改变原有数组或字符串的方法

  • slice(start[, end]): 返回切割的字符串,startend可为负数
  • substring(start[, end]): 返回切割的字符串,startend不可为负数
  • substr(start[, length]): 返回切割的字符串,start可为负数
  • split(): 指定分隔符分割成数组,分隔符可用字符串或正则
  • filter: 过滤器,返回符合要求的数组
  • forEach: 遍历数组,对每个元素进行操作,返回值为 undefined
  • reduce: 求和方法
  • map: 对原有数组进行操作,返回一个新数组

3、记一些别的常用的方法

  • findIndex: 返回符合要求的第一个索引
  • find: 返回符合要求的第一个item
  • some: 若有满足条件的值则返回true,否则返回false
  • every: 若所有值都满足条件返回true,否则返回false
  • includes: 数组中若包含某一项返回true,否则返回false

image.png

image.png

Guess you like

Origin juejin.im/post/7078496313435750413