### JS工具库

128 阅读1分钟

数组去重

function removeRepeatArr(arr) {
    return arr.filter((item, index) => {
        return arr.indexOf(item) === index;
    })
}

对象数组去重

function removeRepeatObjArr(arr, property) {
    return arr.filter((item, index) => {
        return arr.findIndex(item1 => {
            return item1[property] == item[property];
        }) === index;
    })
}

安全获取对象属性值

const getValue = (o, p) => {
	const keys = JSON.stringify(p).match(/[^.,\s\[\]\'\"]+/g);
	return keys.reduce((xs, x) => (xs && xs[x] ? xs[x] : null), o)
}