关于前端:前端面试题题目

4次阅读

共计 2429 个字符,预计需要花费 7 分钟才能阅读完成。

面试题

1.

{a:1, b:2, c:3} 转成 [{a:1}, {b:2}, {c:3}]

function objToArr(obj) {const arr = [];
    const Obj = {};
    for (const key in obj) {if (Object.prototype.hasOwnProperty.call(obj, key)) {Obj[key] = obj[key];
        }
    }
    arr.push(Obj);
    return arr;
}

2 对象数组排序

var presonArr = [{ name:'freddy', age:24, score:97},
    {name:'nick', age:18, score:87},
    {name:'mike', age:26, score:80},
    {name:'james', age:34, score:90},
    {name:'curry', age:30, score:83}
];
// 年龄升序排序
presonArr.sort(function(a,b){return a.age - b.age;});
console.log(presonArr);

3.

var arr1 =[{a:1}, {b:2}, 3]
var arr2 = [].concat(arr1);  // arr2 = […arr1];
 
arr1 === arr2;  // ?
arr1[0] === arr2[0]  // ?
arr1[2] === arr2[2]  // ?

arr1[0].a = 666;
arr2[0].a = ? 

arr1[2] = 888;
arr2[2] = ? 

持续问深拷贝

function deepCopy(data: any, hash = new WeakMap()) {if (typeof data !== 'object' || data === null) {throw new TypeError('传入参数不是对象');
    }
    // 判断传入的待拷贝对象的援用是否存在于 hash 中
    if (hash.has(data)) {return hash.get(data);
    }
    const newData = {};
    const dataKeys = Object.keys(data);
    dataKeys.forEach(value => {const currentDataValue = data[value];
        // 根本数据类型的值和函数间接赋值拷贝
        if (typeof currentDataValue !== 'object' || currentDataValue === null) {newData[value] = currentDataValue;
        }
        else if (Array.isArray(currentDataValue)) {
            // 实现数组的深拷贝
            newData[value] = [...currentDataValue];
        }
        else if (currentDataValue instanceof Set) {
            // 实现 set 数据的深拷贝
            newData[value] = new Set([...currentDataValue]);
        }
        else if (currentDataValue instanceof Map) {
            // 实现 map 数据的深拷贝
            newData[value] = new Map([...currentDataValue]);
        }
        else {
            // 将这个待拷贝对象的援用存于 hash 中
            hash.set(data, data);
            // 一般对象则递归赋值
            newData[value] = deepCopy(currentDataValue, hash);
        }
    });
    return newData;
}

4

// 写出一个数组开展函数, 如输出:[1,[2,[3,4,2],2],5,[6]],  则输入:[1,2,3,4,2,2,5,6] 
// 因为和深度无关,所以说最简略能够这样
function flatten(arr){var res = arr.join().split(',');
    res = res.map(ele => +ele) return res;
} // 还有吗,递归,写一下
function flatten(arr){var array = [];
    arr.forEach(ele => { if(Array.isArray(ele)){array.push(...flatten(ele));
        } else {array.push(ele);
        }
    }) return array;
}

5

// 1. 实现 Promise.all()
https://segmentfault.com/a/11…

6 reduce 及 reduce 数组去重

5、Array.reduce() — 不扭转原数组

reduce(function(pre,cur,index,array){}, initValue)
pre 相当于一个容器,寄存每次执行的后果;每次 return 的值会作为下次的 pre。
回调函数第一次执行时,previousValue 和 currentValue 能够是一个值,如果 initialValue 在调用 reduce 时被提供,那么第一个 previousValue 等于 initialValue,并且 currentValue 等于数组中的第一个值;如果 initialValue 未被提供,那么 previousValue 等于数组中的第一个值,currentValue 等于数组中的第二个值。

var arr = [1,2,3,4];
arr.reduce(function(previousValue, currentValue){return previousValue * currentValue;}); // 24
console.log(arr); // [1,2,3,4]
// 数组去重
var arr = [1,2,3,2,3,4];
var newarr = arr.reduce(function(acc, cur){!acc.includes(cur) && acc.push(cur);
  return acc;
},[]); 
console.log(newArr); // [1,2,3,4]
正文完
 0