面试篇JavaScript编程篇

83 阅读1分钟

js有几种数据类型

  • js有八种数据类型
    • 基本数据类型
      • Number
      • Undefined
      • Null
      • Boolean
      • Symbol(ES6)
        • 这种数据类型主要用于创建一个独一无二的标识,不废话,上代码
        let a = 2
        let obj = {}
        let c = Symbol(2)
        obj[c] = 25
        console.log(a==c)  //false
        console.log(obj)  // {Symbol(2):25}
        
      • String
      • BigInt(ES10)
        • BigInt是一个内置对象,它提供了大于最大安全数的计算方法。
    • 引用数据类型
      • Object
      • Array
      • Function

js如何获取url后的参数

    let obj = {}
    function getQuery(){
        //window.location.search是获取url后的?之后的字符串
        let url = window.location.search.substr(1)
        let query = url.split('&')
        query.map(item=>{
            let queryAry = item.split('=')
            obj[queryAry[0]] = queryAry[1]
        })
        return obj
    }

用js实现div向右平滑300px,耗时一秒,不能使用transition

<div id="element" style="width: 200px; height: 200px; background: #000">我是个方块</div>
<script>
    let ele = document.getElementById('element')
    ele.style.position = 'absolute'
    for(let i = 0; i < 300; i++){
        setTimeout(()=>{
           ele.style.left = i+1+'px'
        },i*1000/300)
    }
</script>

输出在3个数组里都出现的数字

let ary1 = [1,2,3,4]
let ary2 = [2,3,5,6]
let ary3 = [3,2,5,4]
function diffArray(ary1,ary2,ary3){
    let arr = []
    ary1.forEach((item,index)=>{
        ary2.forEach((item2,index2)=>{
            ary3.forEach((item3,index3)=>{
                if(item==item2 && item == item3){
                    arr.push(item)
                }
            })
        })
    })
    return arr
}
diffArray(ary1,ary2,ary3) [2,3]

数组扁平化

  • 使用reduce递归法,reduce可以接受默认值,其参数第一项就是默认传参,第一项就是当前项
    let ary = [1, [25, 23, 6, [256, [254, [35]]]], 3]
    function flaten(ary){
        return ary.reduce((result,now)=>{
            return result.concat(Array.isArray(now)?flaten(now):now)
        },[])
    }
    console.log(flaten(ary))

取数组的中值

```
```