【问题标题】:Filtering duplicate hashes from array of hashes - Javascript从哈希数组中过滤重复的哈希 - Javascript
【发布时间】:2019-05-21 18:48:40
【问题描述】:

我有一个哈希数组,如下所示:

[{id: "4bf58dd8d48988d110941735", name: "italy"},
 {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
 {id: "4bf58dd8d48988d147941735", name: "diner"},
 {id: "4bf58dd8d48988d110941735", name: "italy"},
 {id: "4bf58dd8d48988d1c4941735", name: "resto"},
 {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
 {id: "4bf58dd8d48988d1ce941735", name: "fish"},
 {id: "4bf58dd8d48988d1c4941735", name: "resto"},
 {id: "4bf58dd8d48988d1c4941735", name: "resto"}]

我想丢弃重复的哈希值。 Set 不起作用,因为哈希是唯一的对象。

我觉得卡住了,需要好好思考一下。请指教!

【问题讨论】:

  • 将数组缩减为一个对象,使用id作为key,然后使用Object.values()转换回数组。
  • 你试过什么?
  • 您必须遍历数组以获取每个可能的哈希值并检查、删除。或者更改数据格式,使用带有哈希字符串的对象作为键,名称和其他任何内容作为值的单独对象
  • 只允许纯JS?
  • @hindmost,是的,这是我的 react 应用程序的一部分

标签: javascript arrays hash


【解决方案1】:

你也可以使用reduce

//I added comma to each object
const data= [{id: "4bf58dd8d48988d110941735", name: "italy"},
    {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
    {id: "4bf58dd8d48988d147941735", name: "diner"},
    {id: "4bf58dd8d48988d110941735", name: "italy"},
    {id: "4bf58dd8d48988d1c4941735", name: "resto"},
    {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
    {id: "4bf58dd8d48988d1ce941735", name: "fish"},
    {id: "4bf58dd8d48988d1c4941735", name: "resto"},
    {id: "4bf58dd8d48988d1c4941735", name: "resto"}]

const result= data.reduce((current,next)=>{   
    if(!current.some(a=> a.name === next.name)){
        current.push(next);
    }
    return current;
},[])
console.log(result);

【讨论】:

    【解决方案2】:

    试试这个

    h.filter(( t={}, a=>!(t[a.id]=a.id in t) ))
    

    输入数组h,时间复杂度O(n),解释here.

    let h = [{id: "4bf58dd8d48988d110941735", name: "italy"},
     {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
     {id: "4bf58dd8d48988d147941735", name: "diner"},
     {id: "4bf58dd8d48988d110941735", name: "italy"},
     {id: "4bf58dd8d48988d1c4941735", name: "resto"},
     {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
     {id: "4bf58dd8d48988d1ce941735", name: "fish"},
     {id: "4bf58dd8d48988d1c4941735", name: "resto"},
     {id: "4bf58dd8d48988d1c4941735", name: "resto"}]
     
     let t; // declare t to avoid use global (however works without it too)
     let r= h.filter(( t={}, a=>!(t[a.id]=a.id in t) ))
    
     
     console.log(JSON.stringify(r));

    【讨论】:

      【解决方案3】:

      空间换时间

      let arr = [
          { id: '4bf58dd8d48988d110941735', name: 'italy' },
          { id: '4bf58dd8d48988d1c6941735', name: 'skandi' },
          { id: '4bf58dd8d48988d147941735', name: 'diner' },
          { id: '4bf58dd8d48988d110941735', name: 'italy' },
          { id: '4bf58dd8d48988d1c4941735', name: 'resto' },
          { id: '4bf58dd8d48988d14a941735', name: 'vietnam' },
          { id: '4bf58dd8d48988d1ce941735', name: 'fish' },
          { id: '4bf58dd8d48988d1c4941735', name: 'resto' },
          { id: '4bf58dd8d48988d1c4941735', name: 'resto' }
      ]
      
      let map = {};
      let rest = arr.filter((item) => {
          if(map[item.id] === void 0) {
              map[item.id] = item.id;
              return true;
          }
      });
      map = null;
      
      console.log(rest);

      【讨论】:

        【解决方案4】:

        我建议使用关联数组的方法,这使得重复删除更容易。如果可以的话,您应该首先将您的数组构建为关联数组,这样您就不必转换它。这是你的做法:

        var array = [{
            id: "4bf58dd8d48988d110941735",
            name: "italy"
          },
          {
            id: "4bf58dd8d48988d1c6941735",
            name: "skandi"
          }, {
            id: "4bf58dd8d48988d147941735",
            name: "diner"
          }, {
            id: "4bf58dd8d48988d110941735",
            name: "italy"
          }, {
            id: "4bf58dd8d48988d1c4941735",
            name: "resto"
          }, {
            id: "4bf58dd8d48988d14a941735",
            name: "vietnam"
          }, {
            id: "4bf58dd8d48988d14a941735",
            name: "fish"
          }, {
            id: "4bf58dd8d48988d1c4941735",
            name: "resto"
          }, {
            id: "4bf58dd8d48988d1c4941735",
            name: "resto"
          }
        ];
        
        // you can access the array with arrayAssociative[id], where the id is the real id like "4bf58dd8d48988d110941735"
        var arrayAssociative = {};
        for (item in array) {
          // first get the unique id's
          var addedNode = arrayAssociative[array[item].id] = arrayAssociative[array[item].id] || {};
          if (addedNode.names == null)
            addedNode.names = {};
          // now get the unique names
          var addedName = arrayAssociative[array[item].id].names[array[item].name] = arrayAssociative[array[item].id].names[array[item].name] || {};
        }
        console.log(arrayAssociative);

        我不知道确切的原因,为什么这条线

        var 元素 = arrayAssociative[id] =arrayAssociative[id] || {};

        为此工作,但让我们接受功能原样:)

        【讨论】:

        • arrayAssociative[array[item].id] || {} 如果不是 null/undefined/0/false,则此行在 || 左侧给出值,如果相反,则在右侧给出值 - stackoverflow.com/q/2100758/860099
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-18
        • 2018-08-26
        • 2018-07-27
        • 2011-04-20
        • 1970-01-01
        • 1970-01-01
        • 2023-04-09
        相关资源
        最近更新 更多