// 防抖
function debounce(fn,t){
let timer = null
return function (){
if(timer) {
clearTimeout(timer)
}
timer = setTimeout(() =>{
fn.apply(this,arguments)
},t)
}
}
// 节流
function throttle(fn,t){
let timer = null
return function (){
if(timer) {
return
}
timer = setTimeout(function() {
fn.apply(this,arguments)
},t)
}
}
// 去重
function unique(arr) {
return Array.from(new Set(arr))
}
console.log(unique([1,1,2,2,333,4,4,5]),'去重1 ')
function unique2(arr) {
return [...new Set(arr)]
}
console.log(unique2([1,1,2,2,333,4,4,5]),'去重2222 ')
function unique3(array) {
// 定义要返回的数组
const resultArr = []
// 定义一个对象
const obj = {}
// 循环遍历未去重的数组
array.forEach(item => {
// 如果obj中没有改元素属性
if (!obj.hasOwnProperty(item)) {
// 没有就为obj添加这个属性
obj[item] = true
resultArr.push(item)
}
})
return resultArr
}
console.log(unique3([1,1,2,2,333,4,4,5]),'去重2222 ')
function unique1(arr) {
const resultArr = []
arr.forEach(item=> {
if(resultArr.indexOf(item)===-1) {
resultArr.push(item)
}
})
return resultArr
}
console.log(unique1([1,1,2,2,333,4,4,5]),'去重1111 ')
// 深拷贝
function clone(obj) {
var temp = {}
for (var key in obj) {
if(typeof obj[key] == 'object') {
temp[key] = clone(obj[key])
} else {
temp[key] = obj[key]
}
}
return temp
}
//浅拷贝
function simpleClone(initObj) {
var obj = {}
for (var i in initObj) {
obj[i] = initObj[i]
}
return obj
}
var obj = {
a: "hello",
b:{ a: "world", b: 21 },
c:["Bob", "Tom", "Jenny"],
d:function() {
alert("hello world");
}
}
var cloneObj = simpleClone(obj);
console.log(cloneObj.b);
// 冒泡 冒泡排序就是把小的元素往前调或者把大的元素往后调。 比较是相邻的两个元素比较,交换也发生在这两个元素之间。
function sort(arr) {
for(var i=0;i<arr.length-1;i++) {
for (var j=0;j<arr.length-1-i;j++) {
if(arr[j] > arr[j+1]) {
var x=arr[j]
arr[j] = arr[j+1]
arr[j+1] = x
}
}
}
return arr
}
console.log(sort([2,1,4,3]),'=============>')
// 选择排序 首先在未排序的数组中找到最小项,并移动到数组起始位置,之后对其它未排序的元素继续遍历,接着找最小的元素,放在已排序的元素后面,直到所有数组排序完成。
// 选择排序
function selectionSort(arr) {
var minIndex = 0
for (var i = 0; i < arr.length-1; i++) { // 长度-1,是因为剩下最后一个数时不用做判断
minIndex = i
for (var j = i + 1; j < arr.length; j++) { // j = i + 1 是因为自身不需要做比较
if (arr[j] < arr[minIndex]) {
minIndex = j
}
}
var temp = arr[i]
arr[i] = arr[minIndex]
arr[minIndex] = temp
}
return arr
}
// 数组扁平化
const arr = [1,2,[3,4],[5,[6,[7]]]]
let res = arr.flat(Infinity)
console.log(res,'==========>')
function func(arr) {
let newArr = []
const rec = (arr) => {
arr.forEach(item => {
if(!Array.isArray(item)){
newArr.push(item)
} else {
rec(item)
}
})
}
rec(arr)
return newArr
}
let res2 = func(arr)
console.log(res2,'=============res2')
function flatten(arr) {
let result = []
for(var i = 0 ;i<arr.length;i++) {
if(Array.isArray(arr[i])) {
result = result.concat(flatten(arr[i]))
} else {
result = result.concat(arr[i])
}
}
return result
}
console.log(flatten(arr),'=============33333')
// 求最大公共部分
function findSubstr(str1,str2){
if(str1.length>str2.length){
[str1,str2] = [str2,str1]
}
let result = []
const len = str1.length
for(var j= len; j>0;j--) {
for(var i = 0; i<len-j;i++) {
result = str1.substr(i,j)
if(str2.includes(result)) return result
}
}
}
前端面试基础算法题
最新推荐文章于 2024-07-24 23:00:08 发布