1.new Set()------数组去重
const arr = [1,2,3,3,3,3,4,5]
console.log(new Set(arr)) // {1,2,3,4,5}
const a = Array.from(new Set(arr)) //[1,2,3,4,5]
//或者
const b = [...new Set(arr)]//推荐使用
2.sort() ----- 对数组元素排序(改变原来数组)
const arr = [3,1,35,7,34,2]
console.log(arr.sort((a,b)=> a-b)) //[1,2,3,7,34,35]
console.log(arr.sort((a,b) => b-a))//[35,34,7,3,2,1]
- delete ----删除一个数组成员,会形成空位,并不会影响length属性(改变原数组),可以跟compact搭配使用,去除空位。delete同样适用对象
//数组
const arr [1,2,3]
delete arr[1]
console.log(arr)//[1,empty,3]
//对象
const obj = {name:"周杰伦",age:"3",sex:"男"}
delete obj.sex
console.log(obj)//{name:"周杰伦",age:"3"}
4.toString()—把数组转为字符串
const arr = [2,1,3,4,2,1]
console.log(arr.toString())//2,1,3,4,2,1
5.join() ----- 以其参数作为分隔符,将所有参数组成一个字符串返回(默认逗号分隔)。一般用在给后端传参,将数组拆分成字符串,或者展示的时候
6.for in,for in 遍历对象返回的是对象的key值,遍历数组返回的是数组的下标
const obj = {a:1,b:2}
for (let i in obj){ console log (i)} // a,b
const arr = [1,2,3,4]
for (let i in arr ){ console log (i)} // 0,1,2,3
7.indexOf() 返回给定元素在数组中第一次出现的位置,如果没有则返回-1(适用字符串)
lastIndexOf():返回给定元素在数组中最后一次出现的位置,没有返回-1(适用字符串)
//数组
const arr = [1,2,3,3,4,5,6,7,]
console.log(arr.indexOf(3))//2
console.log(arr.indexOf('3'))//-1
console.log(arr.lastIndexOf(3))//3
//字符串
const str = 'qweqdasdsad'
console.log(str .indexOf('q))//0
console.log(str .lastIndexOf(‘d’))//10
8.flat()数组扁平化,不管一个数组里嵌套多少数组,flat最后都会变为一个以为数组
const arr = [1,2,[3,4,5],[6,7]]
console.log(arr.flat())//[1,2,3,4,5,6,7]
9.Array.isArray() 判断是不是数组,true/false
const arr = [1,3,4,5]
console.log(Array.isArray(arr))//true
10.find() 返回符合传入测试(函数)条件的数组元素,如果没有符合的返回undefined。有就是返回符合条件的第一个元素
cosnt arr = [1,2,3,4,5,6]
const a = arr.find(item=> item > 3)//4
const b = arr.find(item => item === 0)
console.log(b)//undefined
-------------------------------分隔符(String)----------------------------
1.char At() 返回指定位置的字符
const str = 'hello clj'
console.log(str.charAt(3))//l
2.split() 将字符串切割成数组
const str = 'hello'
console.log(str.split(''))//["h","e","l","l","0"]
conlose.log(str.split('',2))//["h","e"]
//通过逗号分割成数组
const str = 'hello , hai'
console.log(str.split(','))//["hello ","hai"]
3.toLocalLowerCase() & toLowerCase()
转小写
toLocalUpperCase() & toUpperCase()
转大写
- .trim() 去掉字符串两端的空格
const str = ' hello '
console.log(str.trim())//hello
--------------------------JSON-----------------------------------
1.JSON.parse() 用于字符串转对象
const str = '{"name" : "mos" , "age":20}'
const obj = JSON.parse()//{name:"mos",age:20}
2.JSON.stringfy() 对象转字符串 ,与JSON.parse()相反
判断一个值的类型的办法
1.typeOf() 用来检测数据类型(无法区分null,Array 和通常意义上的object)
typeof 123 //number
typeof '123' //string
typeof true // boolean
typeof false //boolean
typeof undefined // undefined
typeof Math.abs // function
typeof function () {} // function
// 当遇上`null`、`Array`和通常意义上的`object`,都会返回 object
typeof null // object
typeof [] // object(所以判断数组时可以使用Array.isArray(arr))
typeof {} // object
// 当数据使用了new关键字和包装对象以后,数据都会变成Object类型,不加new关键字时会被当作普通的函数处理。
typeof new Number(123); //'object'
typeof Number(123); // 'number'
typeof new Boolean(true); //'object'
typeof Boolean(true); // 'boolean'
typeof new String(123); // 'object'
typeof String(123); // 'string'
2.instanceOf()运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链。
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
const auto = new Car('Honda', 'Accord', 1998);
console.log(auto instanceof Car); // true
console.log(auto instanceof Object); // true
3.Object.Prototype.toString()
可以精准的判断对象类型。
对于 array、null、object 来说,其关系错综复杂,使用 typeof 都会统一返回 object 字符串,要想区别对象、数组、函数单纯使用 typeof 是不行的,想要准确的判断对象类型,推荐使用 Object.Prototype.toString(), 它可以判断某个对象值属于哪种内置类型
const arrs = [1,2,3];
console.log(typeof arrs) // object
console.log(Object.Prototype.toString.call(arrs)) // [object Array]
---------------------------------改变函数的this对象----------------------------------------------
call(),apply(),bind
const arr = [2,5,4,7,6]
const a = Function.prototype.call.apply(Math.max, arr)
console.log(a) // 7
//如果apply的第二个参数是个null,会返回-Infinity
const b = Function.prototype.call.apply(Math.max, null, arr)
console.log(b) // -Infinity
const fruits = {
"name": "apple",
getOtherFriut: function() {
return this.name;
}
}
const color = {
"name": " is red"
}
const fruitColor = fruits.getOtherFriut.bind(this, color)
console.log(fruitColor()) //is red