利用ES5当中reduce属性即可。
不知道在ES6当中是否有更好的解决方法。贴上方法,谨防自己忘记:
export function arrayUnqiue (arr, name) { // 传入参数 数组, 对象的名称(即对象数组里的属性) let hash = {} return arr.reduce((item, next) => { // eslint-disable-next-line hash[next[name]] ? '' : hash[next[name]] = true && item.push(next) return item }, []) }
例子:
那么调用方式为
arrayUnqiue (arr,'field')