如何删除数组中的重复项?

时间:2019-07-11 09:43:18

标签: javascript jquery

我有任何看起来像这样的数组:

[
    { "tempValue": "Sunday  CLOSE to CLOSE" }, 
    { "tempValue": "Sunday  CLOSE to 08:30" }, 
    { "tempValue": "Sunday  CLOSE to 08:30" }, 
    { "tempValue": "Tuesday  CLOSE to 08:30" }, 
    { "tempValue": "Thursday  CLOSE to 08:30" }
]

我需要从该数组中删除所有重复对象。在上面的示例中,重复项为Sunday ...

所以我尝试了这样的事情:

function unique(list) {
    var result = [];
    $.each(list, function(i, e) {
        if ($.inArray(e, result) == -1) result.push(e);
    });
    return result;
}


unique(hours);

变量hours是我的数组。

但是我上面的代码不起作用!

有人可以请教这个问题吗?

编辑:

所需的输出是这样:

[
    { "tempValue": "Tuesday  CLOSE to 08:30" }, 
    { "tempValue": "Thursday  CLOSE to 08:30" }
]

4 个答案:

答案 0 :(得分:2)

我首先要计算每天发生的次数,然后创建一个仅包含发生次数不止一次的天的数组。然后,您可以根据该数组中是否包含数组项中的日期来.filter

const input = [{
    "tempValue": "Sunday  CLOSE to CLOSE"
}, {
    "tempValue": "Sunday  CLOSE to 08:30"
}, {
    "tempValue": "Sunday  CLOSE to 08:30"
}, {
    "tempValue": "Tuesday  CLOSE to 08:30"
}, {
    "tempValue": "Thursday  CLOSE to 08:30"
}];

const dayCountObj = input.reduce((a, { tempValue }) => {
  const day = tempValue.match(/^\S+/)[0];
  a[day] = (a[day] || 0) + 1;
  return a;
}, {});
const daysToRemove = Object.entries(dayCountObj)
  .filter(([, count]) => count > 1)
  .map(([day]) => day);
  
const output = input.filter(({ tempValue }) => {
  const day = tempValue.match(/^\S+/)[0];
  return !daysToRemove.includes(day);
});
console.log(output);

如果要将计算复杂度降低到O(N)而不是O(N^2),请对daysToRemove使用Set而不是数组:

const input = [{
    "tempValue": "Sunday  CLOSE to CLOSE"
}, {
    "tempValue": "Sunday  CLOSE to 08:30"
}, {
    "tempValue": "Sunday  CLOSE to 08:30"
}, {
    "tempValue": "Tuesday  CLOSE to 08:30"
}, {
    "tempValue": "Thursday  CLOSE to 08:30"
}];

const dayCountObj = input.reduce((a, { tempValue }) => {
  const day = tempValue.match(/^\S+/)[0];
  a[day] = (a[day] || 0) + 1;
  return a;
}, {});
const daysToRemove = new Set(
  Object.entries(dayCountObj)
    .filter(([, count]) => count > 1)
    .map(([day]) => day)
);
  
const output = input.filter(({ tempValue }) => {
  const day = tempValue.match(/^\S+/)[0];
  return !daysToRemove.has(day);
});
console.log(output);

答案 1 :(得分:1)

您也可以尝试这种方式:

var list  = [{"tempValue": "Sunday  CLOSE to CLOSE"}, {"tempValue": "Sunday  CLOSE to 08:30"}, {"tempValue": "Sunday  CLOSE to 08:30"}, {"tempValue": "Tuesday  CLOSE to 08:30"}, {"tempValue": "Thursday  CLOSE to 08:30"}];    
    function removeDuplicate(json){
    var uniques=[];
    var stringify={};
    for(var i=0;i<json.length;i++){
       var keys=Object.keys(json[i]);
       keys.sort(function(a,b) {return a-b});
       var str='';
        for(var j=0;j<keys.length;j++){
           str+= JSON.stringify(keys[j]);
           str+= JSON.stringify(json[i][keys[j]]);
        }
        if(!stringify.hasOwnProperty(str)){
            uniques.push(json[i]);
            stringify[str]=true;
        }
    }
    return uniques;
}

var list = removeDuplicate(list);

console.log(list);

答案 2 :(得分:1)

这只是您的代码,仅包含另外两个命令:

function unique(list) {

  var result = [];
  var resultJson = [];

  $.each(list, function(i, e) {
    if ($.inArray(e.tempValue, result) == -1) {
     result.push(e.tempValue);
     resultJson.push(e);
    }
  });
  return resultJson;
}

 unique(hours);

答案 3 :(得分:0)

单行解决方案可以按以下方式实现:

const unique =(小时)=> [...新设置(小时)];

唯一(小时);