We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
携程
一面
1.数组去重 2.数组扁平化
The text was updated successfully, but these errors were encountered:
// 本质上是利用一层显式遍历和indexOf的隐式遍历,将独一无二的元素放入新数组中返回 Array.prototype.distinct01 = function () { const arr = [] this.forEach(item => { if (arr.indexOf(item) === -1) { arr.push(item) } }) return arr }
// 本质上利用对象来判别元素的唯一性 Array.prototype.distinct02 = function () { const arr = [] const obj = {} this.forEach(item => { if(!obj[item]){ arr.push(item) obj[item] = true } }) return arr }
// 利用Set自动去重的特性和from将其转回数组 Array.prototype.distinct03 = function(){ return Array.from(new Set(this)) }
// 同样使用Set,并适用rest将其展开 Array.prototype.distinct04 = function () { return [...new Set(this)] }
// 只要判断是数组就进入递归 Array.prototype.flat01= function () { return this.reduce((pre, item) => { return pre.concat(Array.isArray(item) ? item.flat01() : item) },[]) }
// 利用some进行判断,只要数组中存在数组,即合并数组,直至循环至数组中无数组 Array.prototype.flat02 = function () { let arr = this while (arr.some(item => Array.isArray(item))) { arr = [].concat(...arr) } return arr }
Sorry, something went wrong.
ChuZexx
No branches or pull requests
面试公司:
携程
面试环节:
一面
问题:
1.数组去重
2.数组扁平化
The text was updated successfully, but these errors were encountered: