1、数组去重的方法;
2、实现一个快排;
3、如何实现a==5&&a==6&a==7;
4、实现一个数组的乱序;
5、斐波那切数列;
6、首字母大写【tt_tt_bt_tt】
7、字符串中出现次数最多的字符【abcdefgeeggdddds】
8、一维数组生成多维数组;
9、找出一个数组中第几大的元素,并算出出现几次;
先自己思考一下,自己的思考才是最重要的;
答案:
1、数组为纯数字: let arr = [1,2,3,4,5,4,2,3,4]
let a = [...new Set(arr)]
或者
let b = arr.reduce((all, after) => {
if(!all.includes(after)){
all.push(after)
}
return all
},[])
数组元素为对象:const objs = [{ id: 1, a: 1 }, { id: 2, a: 2 }, { id: 3, a: 3 }, { id: 1, a: 4 } ];
let objArr = objs.reduce((acc, cur) => {
let ids = acc.map(v => v.id)
return ids.includes(cur.id) ? acc : [...acc, cur]
},[])
log(objArr)
2、快排: let arr = [9,6,7,5,8,2,1,3,4]
function quickSort (arr) {
if(arr.length <= 1){
return arr
}
let index = Math.floor(arr.length/2)
let value = arr.splice(index, 1)[0]
let left = []
let right = []
arr.forEach(v => {
v > value ? right.push(v) : left.push(v)
})
return quickSort(left).concat(value, quickSort(right))
}
3、
var a = 5
a = {
init: 4,
toString: function(){
this.init = this.init + 1
log(this.init)
return this.init
}
}
if(a == 5 && a == '6' && a == '7') {
console.log('yes!')
}
4、实现数组乱序 : let arr = [1,2,3,4,5,6,7,8,9]
function randomList (arr) {
let len = arr.length
while (len) {
let i = Math.floor((Math.random() * len--))
let temp = arr[len];
arr[len] = arr[i];
arr[i] = temp;
}
return arr;
}
5、斐波那切数列 1,1,2,3,5,8,13
function fibonacci (num) {
if(num === 1 || num === 2) {
return 1
}
let nums = fibonacci(num-1)+fibonacci(num-2)
return nums
}
log(fibonacci(7))
function fibonacci (num) {
let num1 = 1;
let num2 = 1;
for (let i = 2; i < num; i++) {
[num1, num2] = [num2, num1 + num2]
}
return num2
}
log(fibonacci(7))
6、首字母大写
var name = 'tt_tt_bt_tt'
function up (name) {
let names = null
names = name.replace(/_([a-z])/g, (a, i) => i.toUpperCase())
return names
}
up(name)
7、字符串出现次数最多元素
var str = 'abcdefgeeggdddds'
function max (str) {
let obj = {}
let arr = []
let keys = null
let num = 0
for (let index = 0; index < str.length; index++) {
const item = str[index];
if(!arr.includes(item)) {
arr.push(item)
obj[item] = 1
} else {
obj[item]++
}
}
for (const key in obj) {
let value = obj[key]
if(value > num) {
num = value
keys = key
}
}
log(num, keys)
}
max(str)
8、一维数组变为多维数组
// [[1,2,3,4,5,6,7],[1,2,3,4,5,6],[1,2,3,4,5],[1,2,3,4]]
var arr = [1,2,3,4,5,6,7,1,2,3,4,5,6,1,2,3,4,5,1,2,3,4]
function newArr (arr) {
let allArr = []
let max = 0
let arrs = []
let obj = {}
arr.forEach(v => {
if(!arrs.includes(v)) {
arrs.push(v)
obj[v] = 1
} else {
obj[v]++
}
})
for (const key in obj) {
let item = obj[key]
max = item > max ? item : max
}
for(let i = 0; i allArr[i] = [] } for (const key in obj) { let item = obj[key] for(let i = 0; i allArr[i].push(key) } } log(allArr) } newArr(arr) 9、找出一个数组中第几大的元素,并算出出现几次; let arr = [6,6,7,5,5,4,3,4,6,8,6,2,3,1,2,3,4] function max (arr, k) { let arrs = [...new Set(arr)] let arrss = arrs.sort((a,b) => a - b) let value = arrs[arrs.length - k > 0 ? arrs.length - k : 0] let count = 0 arr.forEach(v => { if(v === value) { count++ } }) return { value: value, times: count } } log(max(arr, 1))