current position:Home>Front end array de duplication

Front end array de duplication

2022-06-24 09:43:37You are far from it.

1. new Set Method

 		let arr = [1, 2, 3, 3, 2, 1, 6, 5, 7, 8]
        // let brr = [...new Set(arr)]
        let brr = Array.from(new Set(arr))
        console.log(brr);

2. Array traversal

 		let arr = [1, 2, 3, 3, 2, 1, 6, 5, 7, 8]
        let brr = []
        function fn(arr) {
    
            arr.forEach((v) => {
    
                if (brr.indexOf(v) === -1) {
    
                    brr.push(v)
                }
            })
            return brr
        }
        console.log(fn(arr));

3. reduce duplicate removal

        let arr = [1, 2, 3, 3, 2, 1, 6, 5, 7, 8]
        function fn(arr) {
    
            return arr.reduce((pre, cur) => {
    
                return pre.indexOf(cur) === -1 ? [...pre, cur] : pre
            }, [])
        }
        console.log(fn(arr));

        function fn(arr) {
    
            return arr.reduce((pre, cur) => {
    
                return pre.includes(cur) ? pre : [...pre, cur]
            }, [])
        }
        console.log(fn(arr));

copyright notice
author[You are far from it.],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/175/202206240824353556.html

Random recommended