1.精确判断对象的类型
Object.prototype.toString.call()方法判断数据类型;

例:
Object.prototype.toString.call([1,2]) // 输出"[object Array]"

Object.prototype.toString.call({1,2}) // 输出"[object Object]"

封装方法: 
let isType = type => obj => {
  return Object.prototype.toString.call( obj ) === '[object ' + type + ']';
}

isType('String')('123');        // true
isType('Array')([1, 2, 3]);    // true
isType('Number')(123);            // true

方法二
JavaScript constructor 属性
constructor属性返回此对象的Boolean函数的引用 ---W3school
例: 
var ex = []; 
console.log(ex.constructor == Array) // 输出 true
console.log(Array) // 输出 function Array() { [native code] }

let arr = [], obj = {};

typeof arr // 输出 'object'
arr.constructor == Array// 输出 true

typeof obj // 输出 'object'
obj.constructor == Object // 输出 true
  1. es6新特性去重 Array.from(new Set(arr)) (https://es6.ruanyifeng.com/#docs/set-map)

例:

let arr = [1,2,2,3,3,4,5];
let set = new Set(arr);
Array.from(set)
console.log(set) //Set(5) {1, 2, 3, 4, 5}

// 去除数组的重复成员
[...new Set(array)]
// 去除字符串里面的重复字符。
[...new Set('ababbc')].join('')
// "abc"

一维数组去重方法二
var myArr = [1,3,4,5,6,3,7,4];
console.log(myArr.filter((value,index,arr)=>arr.indexOf(value)===index));
//[ 1, 3, 4, 5, 6, 7 ]

// 方法2:利用reduce方法遍历数组,reduce第一个参数是遍历需要执行的函数,第二个参数是item的初始值

var arr = [{
key: '01',
value: '乐乐'
}, {
key: '02',
value: '博博'
}, {
key: '03',
value: '淘淘'
},{
key: '04',
value: '哈哈'
},{
key: '01',
value: '乐乐'
}];

var obj = {};
arr = arr.reduce(function(item, next) {
obj[next.key] ? '' : obj[next.key] = true && item.push(next);
return item;
}, []);
console.log(arr);
3.数组内两元素互换位置
const swapItems = (arr, index1, index2) => {
  //数组元素互换
  /**

   * @param {array} arr 操作的数组
   * @param {string||number} index1 选中元素的index
   * @param {string||number} index2 移动到的下一个位置
     */

  arr[index1] = arr.splice(index2, 1, arr[index1])[0];
  return arr;
};
4.判断元素是否存在数组中

方法1:

let arr = ['1', '2', '3'];
console.log(arr.includes('2')) // true
5.数组反转 reverse() 方法
let arr = [1, 2, 3]
console.log(arr.reverse()) // [3, 2, 1]

// join()数组拼接成字符串, split()字符串拆分成数组
var addTwoNumbers = function(l1, l2) {
  let a = Number(l1.reverse().join("")); // 342
  let b = Number(l2.reverse().join("")); // 465
  let addData = a + b;
  console.log(addData) // 807
  return addData.toString().split("").reverse();
};
console.log(addTwoNumbers([2,4,3], [5,6,4])) // ['7', '0', '8']
6.splice实现数组的删除,插入,替换
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
一、splice 删除
写法: array.splice(index, n)
index: 数组中需要删除数据的起始位置;
n: 需要删除的元素或数据的个数

二、splice 插入
写法: array.splice(index,0,data1,data2,...)
index: 数组需要插入的起始位置;
0: 删除的个数为0;
data1,data2: 需要插入的元素,用逗号隔开
例: data.splice(index, 0, {title: '标题', id: 1}, {title: 'text', id: 2})

三、splice 替换
写法: array.splice(index, n, data1, data2, ...)
index: 需要替换的元素的起始位置;
n: 需要替换的元素的个数;
data1, ...: 需要插入的元素,用逗号隔开;


slice() 方法可从已有的数组中返回选定的元素。
arrayObject.slice(start,end)
start: 起始下标
end: 结束下标
7.Object.assign实现对象合并
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Objecr.assign方法用于对象的合并,将源对象的所有可枚举属性,复制到目标对象
let obj = {a, 1}, obj1 = {b: 2}, obj3 = {c: 3};
Object.assign(obj, obj1, obj2);
obj // {a: 1, b: 2, c: 3}

如果目标对象与源对象或源对象与源对象有同名属性,则后面的属性会覆盖前面的属性
例 let obj = {a, 1}, obj1 = {b: 2, c: 2}, obj3 = {c: 3};
Object.assign(obj, obj1, obj2);
obj // {a: 1, b: 2, c: 3} obj1中的c:2被obj2中的c:3覆盖

克隆对象
function clone(origin) {
return Object.assign({}, origin);
}

// 分页事件
onPageChange = currPage => {
// params = {current: 1, size: 10 }
let params = Object.assign({}, this.state.params, { current: currPage });
this.setState({ params: params }, () => {
this.getDataSource();
});
};
8.sort()数组排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sort 函数,可以接收一个函数,返回值是比较两个数的相对顺序的值
例:

let arr = [3, 15, 8, 29, 102, 22]

1.默认没有函数 是按照 UTF-16 排序的,对于字母数字 你可以利用 ASCII 进行记忆
console.log(arr.sort()) // [102, 15, 22, 29, 3, 8]

2.带函数的比较
// 从小到大排序
console.log(arr.sort(function(a, b) {
return a - b;
})) // [3, 8, 15, 22, 29, 102]

//从大到小排序
console.log(arr.sort(function(a, b) {
return b - a;
})) // [102, 29, 22, 15, 8, 3]