编写一个通用实用程序,它将从javascript中的任何数组中删除重复项

时间:2018-01-17 15:17:46

标签: javascript jquery arrays

我正在编写一个通用实用程序,它将在javascript中删除数组中的重复项。我有以下代码,其中所需的实际工作。我想把它变成一个实用工具。第一个代码块工作,第二个是实用程序,第三个是我试图应用该实用程序(不起作用 - 数组中的重复项仍然出现)。任何见解都会受到超级赞赏。

//Code without utility - works//
var productArr = [];
for (var p in theData) {
  var theProduct = theData[p];
  var theProductSelect = productArr.filter(function(value, index, self) {
    return self.indexOf(value) === index;
  });
  productArr.push(theProduct.name);
}

//Here's the utility I am trying to write
publicMethods.deDuplicate = function(array, key, type) {
  var providedArray = [];
  var uniqueArray = providedArray.filter(function(value, index, self) {
    return self.indexOf(value) === index;
    return providedArray;
  });
};

//Code with Utility - duplicates still appear
var productArr = [];
for (var p in theData) {
  var theProduct = theData[p];
  productArr.push(theProduct.name);
  publicMethods.deDuplicate(productArr)
}

3 个答案:

答案 0 :(得分:2)

两种选择:

let arr = [1, 2, 3, 4, 1, 2, 3];

const getUnique = (arr) => {
    return Array.from(new Set(arr));
}

const getUniqueAlt = (arr) => {
    return arr.reduce((acc, cur) => {
    if(acc.indexOf(cur) == -1) {
        acc.push(cur);
    }

    return acc;
  }, [])
}

console.log(getUnique(arr))
console.log(getUniqueAlt(arr));

https://jsfiddle.net/z1w775y1/

使用一系列对象

赢得了不错的工作

答案 1 :(得分:1)

使用 Set() used with the spread operator: ... 将Set转换为数组,而Set 必须只包含唯一值。 @Bergi使用Set()指出了一个更容易阅读的替代语句。

Array.from(new Set(array))

演示

var arr = [2, "88c", 22, "v21", 3, 865, "a", "a1", 5521, "v21", 5678, "5678", 22];


function uq(array) {
  return [...new Set(array)];
} 
    
console.log('uq(arr)= '+uq(arr));

function unique(array) {
  return Array.from(new Set(array));
}

console.log('unique(arr)= '+unique(arr));

答案 2 :(得分:1)

对于延迟回复表示抱歉,这让我有点意外。

这将从具有任何类型元素的数组中删除重复项。这包括作为数组和对象的元素。如果您在使用它时遇到任何问题,请告诉我。我尽可能多地测试它。

这个概念真的不那么难。通过递归,通常更多的是在实现该概念时不要越过电线。我们递归测试任何元素是否是数组或对象,然后我们递归地对它们进行字符串化。然后我们创建一个set对象,用字符串化项填充它,将其转换为数组,然后将字符串化元素重新填充到对象或数组。

function uniqueArr(original) {
  let returnable = json_recurse(original);
  function json_recurse(arr) {
    return arr.map((item, ind) => {
      if (Array.isArray(item)) {
        return JSON.stringify(json_recurse(item));
      }
      if (typeof item === "object") {
        return JSON.stringify(item);
      } else return item;
    });
  }
  function inflate_recurse(arr) {
    return arr.map(item => {
      if (Array.isArray(item)) return inflate_recurse(item);
      try {
        var obj = JSON.parse(item);
      } catch (e) {
        return item;
      }
      if(obj) {
      return obj;
      }
      else return item;
    })
  }
  return inflate_recurse(Array.from(new Set(returnable)));
}

function uniqueArr(original) {
  let returnable = json_recurse(original);
  function json_recurse(arr) {
    return arr.map((item, ind) => {
      if (Array.isArray(item)) {
        return JSON.stringify(json_recurse(item));
      }
      if (typeof item === "object") {
        return JSON.stringify(item);
      } else return item;
    });
  }
  function inflate_recurse(arr) {
    return arr.map(item => {
      if (Array.isArray(item)) return inflate_recurse(item);
      try {
        var obj = JSON.parse(item);
      } catch (e) {
        return item;
      }
      if(obj) {
      return obj;
      }
      else return item;
    })
  }
  return inflate_recurse(Array.from(new Set(returnable)));
}

let arr = [
   ["whatever", 5], 
   ["whatever", 5], 
  {
    id: 1
  }, 
  ["whatever", 5, 7], 
  3, 5, 9, 
  "hello", {
    id: 2
  }, 
  {
    id: 3
  }, 
  {
    id: 1
  }, 
  3, 5, 9, 
  "hello", 
  {
    id: 2
  }, 
  7, 35, 50, {
    id: 3
  },
  50
];
console.log(uniqueArr(arr));