自我学习记录(一直会更新?)
1.js获取URL中参数值(自己写的)
function getUrl(url) {
let arr = url.slice(url.indexOf('?') + 1).split('&')
let cc = arr.map(item => {
let obj = {}
obj[item.split('=')[0]] = item.split('=')[1]
return obj
})
return cc
}
var newurl = 'http://dev.cekid.com:9018/r/products?store_code=8002&citycode=320100&cityname=%E5%90%88%E8%82%A5%E5%B8%82&cityid=340100&entityid=8029&entityname=%E5%90%88%E8%82%A5%E7%BE%8E%E6%B8%B8'
getUrl(newurl) // (6) [{…}, {…}, {…}, {…}, {…}, {…}]
复制代码
URL中的查询参数解析成字典对象
?
var newurl = 'http://dev.cekid.com:9018/r/products?store_code=8002&citycode=320100&cityname=%E5%90%88%E8%82%A5%E5%B8%82&cityid=340100&entityid=8029&entityname=%E5%90%88%E8%82%A5%E7%BE%8E%E6%B8%B8'
function parseUrl(url) {
var arr = url.slice(url.indexOf('?') + 1).split('&')
var newArr = arr.reduce((prev, next) => {
let _item = next.split('=')
prev[_item[0]] = _item[1]
return prev
}, {})
return newArr
}
console.log(parseUrl(newurl))
// {store_code: "8002", citycode: "320100", cityname: "%E5%90%88%E8%82%A5%E5%B8%82", cityid: "340100", entityid: "8029", …}
复制代码
2.对象变数组
var data = {a: 1, b: 2, c: 3}
function objToArr(obj) {
var arr = []
for (let i in obj) {
arr.push(obj[i])
}
return arr
}
objToArr(data)
//结果: [1, 2, 3]
复制代码
3.一维数组变二维数组
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
function toDoubleArr(arr, i) {
var newArr = []
arr.forEach((item, index) => {
if (index % i === 0) {
newArr.push([])
}
newArr[newArr.length - 1][index % i] = item
})
return newArr
}
toDoubleArr(a, 4)
复制代码
var arr = [1, 2, 3, 4, 5, 6, 7]
function toDoubleArrs(arr, i) {
var newArr = arr.reduce((prev, next, index) => {
if (index % i === 0) {
prev.push([])
}
prev[Math.floor(index / i)].push(next)
// prev[prev.length - 1].push(next)
return prev
}, [])
return newArr
}
toDoubleArrs(arr, 5)
复制代码
4. 一维数组变三维数组
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
let newArr = arr.reduce((prev, next, index, tmp) => {
if(index % 10 === 0) {
prev.push([])
}
if(index % 5 === 0) {
prev[prev.length - 1].push([])
}
prev[prev.length-1][prev[prev.length-1].length-1].push(next)
return prev;
}, [])
newArr
复制代码
5.数组去重(好同学问的一个问题,自己就回答了前两个,得??)
1.ES6方法: 利用Array.from()
将Set
结构转化为数组Set数据结构,他类似于数组,其特点就是成员值都是唯一的
var arr = [1, 2, 3, 4, 5, 3, 2, 2, '123', '123']
var newArr = Array.from(new Set(arr))
console.log(newArr)
复制代码
补充:
拓展运算符(...)内部使用for...of循环
var arr = [1, 2, 3, 4, 5, 3, 2, 2, '123', '123', '1']
var newArr = [...new Set(arr)]
console.log(newArr)
复制代码
2.indexOf()查找新数组中有没有当前这个值,没有就push
indexof结合forEach、map、filter
var a = [1, 2, 3, 4, 5, 3, 2, 2, '123', '123']
function distinct(arr) {
var arrs = []
arrs.push(arr[0])
arr.forEach((item, index) => {
if (arrs.indexOf(item) === -1) {
arrs.push(item)
}
})
return arrs
}
distinct(a)
复制代码
3.利用对象的属性不能相同
的特点进行去重 错误的写法,没区别字符串'1'和数字1
:
var a = ['a', 2, 3, 2, 1, 3, 4, '1']
function distinct(arr) {
var obj = {}
var newArr = []
arr.forEach((item, index) => {
if (!obj[item] ) { // 如果obj中能够查到的话,说明item在newArr数组中已存在,所以取反
// 查不到情况下,执行数组push,obj记录
obj[item] = 1
newArr.push(item)
}
})
return newArr
}
console.log(distinct(a))
复制代码
正确写法
:
var a = ['a', 2, 3, 2, 1, 3, 4, '1']
function distinct(arr) {
var obj = {}
var newArr = []
arr.forEach((item, index) => {
if (!obj[typeof item + item] ) { // 如果obj中能够查到的话,说明item在newArr数组中已存在,所以取反
// 查不到情况下,执行数组push,obj记录
obj[typeof item + item] = 1
newArr.push(item)
}
})
return newArr
}
console.log(distinct(a))
复制代码
6.去掉字符串前后所有空格
var str = ' 12 123 1212 '
function trim(str) {
return str.replace(/(^\s*)|(\s*$)/g, '') // 前后都去除
// return str.replace(/(^\s*)/g, '')
// return str.replace(/\s*$/g, '')
}
trim(str) // "12 123 1212"
复制代码
str.trim(), str.trimLeft(),str.trimRight()
var str = ' 12 123 1212 '
str.trim()
复制代码
1.1js预处理机制
var c = 1;
function c(c) {
console.log(c);
var c = 3;
}
console.log(c) // 1
console.log(typeof c) // number
c(2) // c is not a function
复制代码
js
语言本身具有预处理机制,js引擎在预处理期对所有声明的变量和函数进行处理,就是先把变量进行声明并读到内存里。
预处理时,变量和函数的优先顺序,先变量后函数。当变量名和函数名一致时后者会覆盖前者
1.2请补充代码,使mySort()能将传入的参数按从小到大的顺序显示出来。
var result = mySort(23,44,56,98,1,2,33);//传入的参数不定
console.log(result);//显示结果
复制代码
解答
:
function mySort(){
var args = new Array();//使用数组作为参数存储容器
args = Array.from(arguments);
args.sort(function(a,b) {
return a-b;
})
return args;//返回已经排序的数组
}
var result = mySort(23,44,56,98,1,2,33);//传入的参数不定
console.log(result);//显示结果 [1, 2, 23, 33, 44, 56, 98]
复制代码
解析: 1.arguments
:对象的长度是由实参个数而不是形参个数决定的。js将传入这个函数的全部参数存储在一个arguments类数组对象中,arguments.callee这个属性里面存储着当前调用的函数的全部代码
2.Array.from():将类似于数组的参数的值转化为真正的数组。
3.array.sort(): 这个是数组排序
1.3 字符串转驼峰写法,例:'border-bottom-color' --> borderBottomColor
var str = 'border-bottom-color'
function toNewStr(str) {
let _newStr = str.split('-').reduce((prev, next) => {
return prev + next.charAt(0).toUpperCase() + next.substring(1)
})
return _newStr
}
toNewStr(str) // "borderBottomColor"
复制代码
?得看看数组、字符串的一些方法
1.数组方法:reduce
2.字符串常见方法:split、charAt()、toUpperCase()、substring()
1.4 找出字符串中出现最多的字符和个数
对象遍历for(var i in obj){ // 执行操作 }
var str = '123sdada31213cscadada'
function getStr(str) {
// 获取字符串和其对应的个数
var obj = {}
str.split('').forEach((item , index) => {
if (obj[item]){
obj[item] = obj[item] + 1
} else {
obj[item] = 1
}
})
return obj
}
function getMax(obj) {
// 从获得的对象中找到最大的值
var maxValue = 0
var maxIndex = 0
for(var i in obj) {
if (maxValue < obj[i]) {
maxValue = obj[i]
maxIndex = i
}
}
return '第' + maxIndex + '个值最大为' + maxValue
}
function getStrMax(str) {
// 执行函数
let _str = getStr(str)
let str_ = getMax(_str)
return str_
}
getStrMax(str)
复制代码