面试记录

86 阅读1分钟

蚂蚁

问答:

react16.8新增了哪些hooks,怎么使用的?

vue2、3的响应式原理,有什么区别,为什么?

Promise all race any 区别、使用场景?

笔试

// a=1,b=2 怎么在不新增变量的前提下,互换值?
//解构
[a,b] = [b,a]
// 能否打印出hello
let flag = false
setTimeout(()=>flag=true,3000)
setTimeout(()=>{
    while(!flag){
        if(flag) break
    }
    console.log('hello')
},0)
// answer 不能,因为 时间设置成0 会先执行
// 将数组转化为树
const list = [ { id: 04, pid: 03 },{ id: 01, pid: null },{ id: 02, pid: null },
{ id: 03, pid: 01 }, { id: 05, pid: 01 }, { id: 06, pid: 03 }, { id: 07, pid: 02 },
{ id: 09, pid: 02 }, { id: 10, pid: 07 }]

function toTree(data) { 
    let result = []; 
    let obj = {}; 
    data.forEach(item => { 
        //遍历之前的pid可能已经将此次遍历到的数据id,放入map中,所以在此将obj[item.id]合入item 获得之前的push的children
        obj[item.id] = Object.assign(item, obj[item.id] || {}); 
        if (item.pid) { 
            let parent = obj[item.pid] || {}; 
            parent.child = parent.child || [];
            parent.child.push(item);
            obj[item.pid] = parent; 
        }
        else { 
            result.push(obj[item.id]) 
        } 
    }) 
    return result; 
}