JSMDN常⽤函数总结/*
2018/08/25 更新⽇志:增加六:条件操作符判断
2018/09/04 更新⽇志:增加六.5.三元运算,六.1.注意下,七、eval函数使⽤⽰例
*/
⼀.数组Array常⽤⽅法
1. 使⽤reduce
const arr = [{
"code": "badge",
"priceList": [{
"amount": 3000
}]
},
{
"code": "DigitalPhoto",
"priceList": [{
"amount": 1990
}]
}
]
let arr2 = duce((pre, cur) => {
de] = cur.priceList
return pre
}, {})
console.log(arr2)
// 打印结果:
// { badge: [ { amount: 3000 } ],
// DigitalPhoto: [ { amount: 1990 } ] }
2. 使⽤map
let arrMap = [{
name: 'aaa',
age: 23,
address: 'henan'
}, {
name: 'bbbb',
age: 26,
address: 'hebei'
}, {
name: 'cccc',
age: 27,
address: 'anhui'
}]
arrMap = arrMap.map(obj => obj.name)
console.log(arrMap) //打印结果:['aaa','bbb','ccc']
3. 使⽤filter(过滤)
let arrFilter = [{
name: 'aaa',
age: 23,
address: 'henan'
}, {
name: 'bbbb',
age: 26,
address: 'hebei'
}, {
name: 'cccc',
age: 27,
address: 'anhui'
}]
arrFilter = arrFilter.filter(obj => obj.age > 23)
console.log(arrFilter) //结果:[26,27]
4. 使⽤sclice截取数组元素
const arr = [1, 3, 5, 7, 'aa'] //删除数组元素,也可以⽤sclice,
let rs = arr.sclice(0, 2)
console.log(rs) //[1,3]
5. delete⽅法删除数组指定元素和josn数据中的值
let user = [{
name: 'aaa',
age: 23,
address: 'henan'
}, {
name: 'bbbb',
age: 26,
address: 'hebei'
}, {
name: 'cccc',
age: 27,
address: 'anhui'
}]
delete user[0] //user中第⼀条数据被删除了
delete user[1].name //use中第⼆条数据的name属性被删除了
console.log(user.join(',')) //将数组变为了字符串,并⽤,号连接,变为字符串后,⽤user.split(','),按,号拆分,⼜可将字符串变为数组6. 数组去除重复
Array.prototype.distinct = function() {
var arr = this,
result = [],
i,
j,
len = arr.length;
for (i = 0; i < len; i++) {
for (j = i + 1; j < len; j++) {
if (arr[i] === arr[j]) {
j = ++i;
}
}
result.push(arr[i]);
}
return result;
}
let arr = [1, 2, 1, 2, 3]
console.log(arr.distinct()) //[1,2,3]
7. 给josn数组去除重复, 然后按照指定字段,给数组排序
let user = [{
name: 'aaa',
age: 23,
address: 'henan'
birthday: '2011-01-01'
}, {
name: 'bbbb',
age: 26,
address: 'hebei',
birthday: '2000-03-01'
}, {
name: 'cccc',
age: 27,
address: 'anhui',
birthday: '2000-03-01'
}, {
name: 'aaa',
age: 23,
address: 'henan'
birthday: '2011-01-01'
}, {
name: 'aaa',
age: 23,
address: 'henan'
birthday: '2011-01-01'
}, {
name: 'cccc',
age: 27,
address: 'anhui',
birthday: '2000-03-01'
}]
for (var i = 0; i < user.length; i++) {
for (var j = i + 1; j < user.length;) {
if (user[i].name == user[j].name && user[i].birthday == user[j].birthday && user[i].address == user[j].address && user[i].birthday == user[j].birthday) { user.splice(j, 1) //删除数组中重复的json数据
} else j++
}
}
//指定数组按照⽣⽇⼤⼩排序
user.sort((p, c) => {
return new Date(c.birthday) - new Date(p.birthday)
})
console.log(user, '数组长度:' + user.length)
8. push ⽅法向数组中增加⼀条数据(shift, pop, unshift, ⽤的不多)
arr.push('xiaoming')
9. concat连接两个数组
var array1 = ['a', 'b', 'c'];
var array2 = ['d', 'e', 'f'];
console.at(array2))
10. 判断是否是数组
Array.isArray([1, 2, 3]) //true
11. 判断数组中是否包含元素includes
var array1 = [1, 2, 3];
console.log(array1.includes(2))
12. forEach循环遍历数组, 也可以使⽤for遍历,参考⽂档
⼆、常⽤字符串操作
1. 字符串截取substr, substring
const str = "abcdefghij";
console.log("(1,2): " + str.substr(1, 2)) // (1,2): bc
2. 去除前后空格trim
const orig = ' aaa ';
console.im()) // 'aaa'
3. match⽅法,全局忽略⼤⼩写匹配包含的字符
var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var regexp = /[A-E]/gi;
js argumentsvar matches_array = str.match(regexp);
console.log(matches_array);
/
/ ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
4. replace 替换字符串中的字符
var re = /apples/gi;
var str = "Apples are round, and apples are juicy.";
var newstr = place(re, "oranges");
// oranges are round, and oranges are juicy.
console.log(newstr);
5. str.split(',') //字符串按照⾃⼰指定的符号拆分,注意,返回的是数组[a,b,c]
6. LowerCase(), UpwerCase() //字符串转换为⼤⼩写ABC=>abc
7. 字符串链接 'a' + 'b' == 'ab', 'a'.concat('b') == 'ab'
8. str.includes('aa') ⽅法⽤于判断⼀个字符串是否包含在另⼀个字符串中,根据情况返回 true 或 false
7. 出⼀个字符串的位置, 'aaccBB'.indexOf('c') //结果是2,字符串索引从0开始
三、⽇期Date操作
1. 获取当前时间
var date = new Date() //date = new Date(‘2019-09-09’)当前时间
const time = Time() //返回毫秒值
const year = FullYear() //年⽉⽇
const month = Month() + 1
const date = Date() //⽇
const hours = Hours()
const minutes = Minutes() //分
const seconds = Seconds() //秒或者毫秒time/1000
const week = Day() //获取是星期⼏
四、条件语句
1.
<
case ....default, do ...
while,
while,
if ..else,
if ...
else if ..else
2. return语句终⽌函数的执⾏,并返回⼀个指定的值给函数调⽤者
3. break终⽌while循环和for循环
4. 使⽤f,
for..in 遍历数组、 json
//遍历json
var obj = {
a: 1,
b: 2,
c: 3
}
for (var key in obj) {
console.log("obj." + key + " = " + obj[key]); //⽤"+"号拼接
console.log(`obj.${key} 对应的值: ${obj[key]}`) // `为ESC下的键
}
//遍历数组f
let arr = [10, 20, 30];
for (const value of arr) {
console.log(value);
}
//遍历数组for..in
for (const i in arr) {
console.log(arr[i]);
}
/
/遍历数组for循环
for (let k = 0; k < arr.length; k++) {
console.log(arr[k]);
}
五、常⽤对象操作
1. JSON.parse(josnStr) //JSON字符串转为json对象 '{name:1}'=>{name:1}
2. JSON.stringify() //json对象转为JSON字符串 {name:1}=>'{name:1}'
3. und(5.9) //返回四舍五⼊后的整数
4. Math.floor(4
5.95) //45
5. il(7.004); // 8
6. parseInt('90.89') //将字符串转换为Number数字
7. Number('90.1089').toFixed(2) //四舍五⼊保留2位⼩数
8. isNaN(5) //5不是数字吗=>false,isNaN('5') '5'=>true,‘5’是字符串
7. 对象继承 Object.assign()
const o = {
a: 1,
b: 2,
c: 3
}
const t2 = Object.assign({
c: 4,
d: 5
}, o);
console.log(t2.c); //c=>3 继承会覆盖原有属性的值,注意继承的顺序
8. Object.keys() //获取json对象的键,并返回数组对象
9. Object.values() //获取json对象的值,并返回数组对象
10.e val() 函数会将传⼊的字符串当做 JavaScript 代码进⾏执⾏。
eval(new String("2 + 2")); // 返回了包含"2 + 2"的字符串对象
eval("2 + 2"); //4
11. js正则表达式
RegExp 构造函数创建了⼀个正则表达式对象,⽤于将⽂本与⼀个模式匹配
a.验证邮箱格式
const regEmail = /^\s*\w+(?:\.{0,1}[\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\.[a-zA-Z]+\s*$/
if (!st('35410@qq')) {
throw error('邮箱格式不对')
}
12. js中的apply(), call(), bind(), callback() //不常⽤,但有⼈⽤,我⼀个痘没⽤过
六、条件判断操作符
1. 判断true, false
注:
false、 0、 NaN、 undefind、 null、 ''
都是false, 即没有值
例如:
/
/ a != false 且 a != 0 且a != null
if (a != false && a != 0 && a != NaN && a != undefind && a != null && a != '') 可以简写为
if( !a)
a 有值的话, 可以写为
if (a) {}
2. 使⽤ || (或者)
const a = 'ab'
// 如果c != false&& c !=0&& c != NaN && c != undefind&& c != null && c != '' 的话,
// 返回c的值;否则,则返回a的值
let b = c || a //结果 b = ab,c没有值
if (b || c) { //如果b有值,或者c有值,执⾏console;此处c没有值,但b有值,也会执⾏console
console.log(b)
}
3. 使⽤ && 进⾏逐级判断
let user = {
name: ['zhang', 'li', 'liao'],
address: '',
email: '2343@qq',
cousrse: 'pragram'
}
//如果user的属性值都为空,或者user={}的话,结果都包含haha,即默认值显⽰
const mobile = bile || '110' // 因为没有mobile属性,直接写bile,会返回 undefind 加了 || 110,则返回 110,即默认值
const cousrse = user && usrse //course ='pragram'
const address = user && user.address || 'haha' //结果 res = 'haha'
const email = user && ail || 'haha@qq' //结果 email ='2343@qq'
const name = user && user.name && user.name.length > 0 ? user.name : ['haha'] //结果name =['zhang','li','liao'],若果name为[],则结果为['haha']
4. 使⽤ && 、 || 综合使⽤
//如果user2.name存在,且user.name 的值存在user.name != false&& user.name !=0&& user.name != NaN && user.name != undefind&& user.name != null && user.name != '' 的话,执⾏console if (user2.name && user.name) {
console.log('a')
}
//如果 user&&user.name 或者 user2&&use2.name 或者user3&&use3.name 有值的话,执⾏console
if ((user && user.name) || (user2 && use2.name) || (user3 && use3.name)) {
console.log('a')
}
5. 三元运算:
let a = 3
let c = a == 3 ? 'c' : a //c=>c
let b = a == 2 ? 'a' : a == 1 ? 'b' : 'c' //if...lse
console.log(b) //b=>c
七、 eval函数使⽤⽰例
1. 正则表达式中使⽤
var name = 'aaa'
eval(`/${name}/ig`) //ig,全局匹配,忽略⼤⼩写,`/${name}/ig`模版语法,字符串类型,eval将字符串当作函数执⾏
2. 使⽤eval执⾏指定函数, ⼀般为动态调⽤function封装
var val = 1,
val2 = 2,
val3 = 3,
v1 = 'a',
v2 = 'b',
v3 = 'c';
eval(`test${val}(val)`); //....test1. (1)
eval(`test${val2}()`); //....
eval(`test${val3}(v1,v2,v3)`); //....0:a , 1:b , 2:c
function test1(val) {
console.log('....' + val);
}
function test2() {
console.log('....');
}
function test3() {
let str = ''
for (let i in arguments) { //arguments为隐式参数,类型为数组
str += i + ':' + arguments[i] + ' '
}
console.log('....' + str);
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论