lalitmee
9/14/2018 - 12:30 PM

Function for removing the Duplicated and sorting the array

Function for removing the Duplicated and sorting the array

    removeDuplicates(myArr, prop) {
        const noResult = {
            label: 'No Result Found',
            value: null
        };
        if (this.data && this.data.length > 0 && myArr && myArr.length === 0) {
            myArr.push(noResult);
            return myArr;
        } else {
            myArr.sort((a, b) => {
                if (a.label < b.label) {
                    return -1;
                }
                if (a.label > b.label) {
                    return 1;
                }
                return 0;
            });
            return myArr.filter((obj, pos, arr) => {
                return arr.map(mapObj => mapObj[prop]).indexOf(obj[prop]) === pos;
            });
        }
    }