1.在src目录下新建一个util目录
2.新建一个CommonUtil.js文件
这里我以集合去重方法为例
//根据code去重
export function removeDuplicate (arr) {
let obj = {};
arr = arr.reduce((newArr, next) => {
obj[next.code] ? "" : (obj[next.code] = true && newArr.push(next));
return newArr;
}, []);
return arr;
}
3.在vue文件中引用
import {removeDuplicate} from '@/util/CommonUtil.js'
let payMethods =[
{
"code": "WX",
"name": "微信"
},
{
"code": "ZFB",
"name": "支付宝"
},
{
"code": "CFT",
"name": "财付通"
}
]
console.log("removeDuplicate",removeDuplicate(payMethods))
标签:arr,Vue,系列,newArr,next,---,code,obj,removeDuplicate
From: https://www.cnblogs.com/hujunwei/p/18290835