比较两个数是否相等 返回值:true/false
- console.log(Object.is(+1,-1)); // false
- console.log(Object.is(+0,-0)); // false
- console.log(+0===-0); // true
- console.log(NaN===NaN); // false
- console.log(Object.is(NaN,NaN)); // true
- // 把第二个参数复制给第一个参数
- let res = Object.assign(obj,obj1);
- console.log(res, obj); //{ name: 'zzy', age: 17 } { name: 'zzy', age: 17 }
- console.log(res===obj); // true
- let obj2 = {};
- let obj3 = {name:"fhj"}
- let obj4 = {gender:"female"};
- let res1 = Object.assign(obj2,obj3,obj4);
- console.log(res1, obj2); // { name: 'fhj', gender: 'female' } { name: 'fhj', gender: 'female' }
- let obj5 = {name:"xiao"};
-
- console.log(obj5.__proto__); // [Object: null prototype] {}
-
- console.log(obj5.constructor.prototype); // [Object: null prototype] {}
-
- console.log(Object.getPrototypeOf(obj5)); // [Object: null prototype] {}
- Object.setPrototypeOf(obj,obj1) // 将obj的原型对象设置为obj1
-
- console.log(obj.__proto__); // { name: 'zzy', age: 17, class: { number: '222' } }
-
- console.log(obj.constructor.prototype); // [Object: null prototype] {}
-
- console.log(Object.getPrototypeOf(obj)); // { name: 'zzy', age: 17, class: { number: '222' } }
- console.log(Object.keys(obj),"返回键值组成的数组"); // [ 'name', 'age', 'class' ] 返回键值组成的数组
- console.log(Object.values(obj),"返回值组成的数组"); // [ 'ts', 17, { number: '222' } ] 返回值组成的数组
- console.log(Object.entries(obj),"返回键值对组成的数组"); // [ [ 'name', 'ts' ], [ 'age', 17 ], [ 'class', { number: '222' } ] ] 返回键值对组成的数组
用于把二维数组还原成对象
- console.log(Object.fromEntries(Object.entries(obj)));
- // { name: 'ts', age: 17, class: { number: '222' } }
将类数组对象转换为数组
console.log(Array.from("hello")); // [ 'h', 'e', 'l', 'l', 'o' ]
创建数组实例
- let a = Array.of(10, "hello");
-
- console.log(a); // [ 10, 'hello' ]
find方法返回第一个满足条件的元素或者undefined, 参数是回调函数
- var arr = [1, 2, 3, 4, 5];
-
- let res = arr.find((item, index, arr) => {
-
- return item > 3;
-
- });
-
- console.log(res); // 4 (元素)
返回第一个满足条件的元素的索引或者-1, 参数是回调函数
- var arr = [1, 2, 3, 4, 5];
- let res1 = arr.findIndex((item, index, arr) => {
-
- return item > 4;
-
- });
- console.log(res1); // 4 (索引)
includes是否包含某个元素,返回true/false
- var arr = [1, 2, 3, 4, 5];
- console.log(arr.includes(300)); // false
用来填充数组,修改原数组
- var arr = [1, 2, 3, 4, 5];
- arr.fill(8); //参数 返回值 修改原数组
- console.log(arr); // [ 8, 8, 8, 8, 8 ]
- var arr1 = [1,2,[3,[4],5],6,7,8]
-
- console.log(arr1.flat(2)); // 参数为层级深度参数(默认为1)
flat + map 通过对map调整后的数据尝试展平操作
- var arr2 = [1,2,3,[4,5,6]]
- // 需求:将数组中的每个元素展平再乘以2
- let b = arr2.flatMap((item)=>{
- if(typeof item==='number'){
- return item*2;
- }else{
- return item.map(item=>{
- return item*2;
- })
- }
- })
- console.log(b); // [ 2, 4, 6, 8, 10, 12 ]
去除前面的空格
- let str = " Taylor Swift ";
- console.log(str + '22');
- console.log(str.trimStart());
去除后面的空格
- let str = " Taylor Swift ";
- console.log(str + '22');
- console.log(str.trimEnd() + '22');
填充字符串, 参数(填充完的字符串长度,填充的字符串)
如果没有第二个参数,则自动用空格填充
- let str = " Taylor Swift ";
- console.log(str.padStart(15)); // Taylor Swift
- console.log(str.padEnd(20,'Alison')); // Taylor Swift Alison
替换
- let str1 = '111222333';
- // 将1替换成x
- console.log(str1.replace('1','x'));
- // 用正则表达式开启g模式全局替换
- console.log(str1.replace(/1/g,'x'));