Spread Operator equivalent in IE - Javascript

后端 未结 5 1495
太阳男子
太阳男子 2020-12-20 22:12

I have a javascript function to populate dropdowns for individual table rows like:

$scope.possibleOptions = getUniqueValues($scope.yypeOptions, \'yypeOption\         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-20 22:36

    The getUniqueValues there is performing two things for you; removing duplicated elements and also cloning the array. However, the map already is a clone of the array, so you just need to remove duplicates; you could use something like this:

    function onlyUnique(value, index, self) { 
        return self.indexOf(value) === index;
    }
    
    function getUniqueValues(array, prop) {
        function mapper(item) {
            return item[prop];
        }
        return array.map(mapper).filter(onlyUnique);
    }
    

    I'd suggest you to take a look at stuff like webpack and babel in order to use the latest JS and also work on IE, by using transpiler and polyfills to generate compatible code ;)

    PS. I don't have IE right now to test if filter works, but I'm pretty sure it does; otherwise you could remove duplicates by hand with a plain old for.

提交回复
热议问题