javascript 删除对象重复元素


一个json 数组,根据日期来对应筛检属性

publishtime 为发布日期, 循环的时候希望创建一个数组 publishtime 只出现一次的值后面如果重复都不出现,比如 May.13现在出现3次,那要实现以后就是 只有第一次出现May.13的时候保留这个字段其他全部删除

[{
"length": "02:50",
"channel": "笑点集装箱",
"vid": "1a665661c9bfdf9",
"title": "如果有一天siri控制了人类…",
"gender": 0,
"image": " http://7xiqwt.com2.z0.glb.qiniucdn.com/image/1eb46ae2ec8bcc1baad174c11... ",
"avator": " http://7xiqwt.com2.z0.glb.qiniucdn.com/image/46755e0014e5a74babae13cee... ",
"click": 42219,
"chid": "21ddc47f57e79f2",
"publishtime": "Now"
},
{
"length": "01:01",
"channel": "冉冉是Vimeo的搬运工",
"vid": "1bfef4c4ccae113",
"title": "如果你还没有吃夜宵,看看这支片",
"gender": 2,
"image": " http://7xiqwt.com2.z0.glb.qiniucdn.com/image/d9b2a7b2a1f402eafcfe2464c... ",
"avator": " http://7xiqwt.com2.z0.glb.qiniucdn.com/image/cf782ac9cd01dd9fe5b62285f... ",
"click": 421,
"chid": "9eb01af2093606e",
"publishtime": "May.13"
},
{
"length": "01:23",
"channel": "为了报仇看电影",
"vid": "e0621d6ece5c448",
"title": "复仇者联盟的生活聚会里都在玩些什么",
"gender": 2,
"image": " http://7xiqwt.com2.z0.glb.qiniucdn.com/image/7be260d5edda9f11f268b75d7... ",
"avator": " http://7xiqwt.com2.z0.glb.qiniucdn.com/image/51c9acd51dce33e06730bb172... ",
"click": 90,
"chid": "85b8379357b2e16",
"publishtime": "May.13"
},
{
"length": "03:53",
"channel": "道理要从街上学",
"vid": "15a6623fb5a5d4b",
"title": "这3个学生对流浪汉做的事感动了1千万人",
"gender": 0,
"image": " http://7xiqwt.com2.z0.glb.qiniucdn.com/image/ff305c1bd0761c87e2c9105f0... ",
"avator": " http://7xiqwt.com2.z0.glb.qiniucdn.com/image/6cd7be40224688f9e2412a7b5... ",
"click": 19665279,
"chid": "3a8048d007fa98e",
"publishtime": "May.13"
}]

json javascript对象 JavaScript

十步摔一跤 10 years ago

我也来一发:


 function filter(target){
    var result = [];
    result.push(target.shift());
    for(var i = 0;i<target.length;i++){
        if(result.length > i && result[i].publishtime != target[i].publishtime){
            result.push(target[i]);
        }
    }
    return result;
}

filter(array);

无聊的寂寞人 answered 10 years ago


 (function aa(list){
    for(var i=0,len=list.length;i<len;i++){
          for(var j=i+1;j<len;j++){
            if(list[i].publishtime===list[j].publishtime){
                list.splice(j,1);
                j--;
                len--;
            }
         }
    }
})(list);

我写了一个,不知道是否完全符合你的要求,试一下吧,第一次评论,吼吼好紧张,别喷我~~

蹭得累才不是那 answered 10 years ago

試一試 Lodash


 var texts = [/* ... */]; 
var uniq = _.uniq(texts, function(text){
        return text['publishtime'];
});

  1. 看看那種寫法更快Perf
2alex2 answered 10 years ago


 function filter(arr,attr){
  var tmp = {},re = [],i=0,len = arr.length;
  for(;i<len;i++){
    if(!(arr[i][attr] in tmp)) re.push(arr[i]);
    tmp[arr[i][attr]] = 1;
  }
  return re;
}

一个人旅行 answered 10 years ago

是想删属性和值么?


 var test = 你的那个数组;
function unique(array, attr){
    var tmp = [];
    for(var i=0, length = array.length; i < length; i++){
        var item = array[i];
        for(var key in item){
            if(attr == key){
                if(tmp.indexOf(item[key]) == -1){
                    tmp.push(item[key]);
                }else{
                    delete item[key];
                }

            }
        }
    }
}
unique(test, "publishtime");

正直丿绅士 answered 10 years ago

Your Answer