过滤相同名称和日期的数组

时间:2014-11-11 10:01:19

标签: javascript arrays

var Array = [{"Name":"Temp","Date":"2014-10-23"},
{"Name":"Temp","Date":"2014-10-22"},
{"Name":"Temp","Date":"2014-10-18"},
{"Name":"Temp","Date":"2014-10-19"},
{"Name":"Temp2","Date":"2014-10-12"},
{"Name":"Temp2","Date":"2014-06-12"}]

在以下条件下过滤上述数组的最佳方法是什么。

*如果名称相同,则过滤掉所有具有相同名称的对象,并仅留下具有最​​新日期的对象。

我能想到的就是做循环。请注意,Date是一个真正的dateObject,而不是我上面写的字符串,这意味着你可以进行日期比较。

6 个答案:

答案 0 :(得分:0)

使用Array.reduce(),我能够将其归结为仅包含Temp和Temp2及其日期的对象:

var obj = array.reduce(function(base,cur){
    if (base[cur.Name]) {
        if (base[cur.Name].Date < cur.Date) {
            base[cur.Name].Date = cur.Date;
        } else {
            return base;
        }
    } else {
        base[cur.Name] = cur;
        return base;
    }
},{});

从那里,您可以使用Object.keys().map()获取对象的值:

array = Object.keys(obj).map(function(k){
    return obj[k];
});

答案 1 :(得分:0)

   var Array = [{"Name":"Temp","Date":"2014-10-23"},
{"Name":"Temp","Date":"2014-10-22"},
{"Name":"Temp","Date":"2014-10-18"},
{"Name":"Temp","Date":"2014-10-19"},
{"Name":"Temp2","Date":"2014-10-12"},
{"Name":"Temp2","Date":"2014-06-12"}]

var result = {};
Array.forEach(function(item) {
  var name = item['Name'];
  if(result[name]) {
    result[name].push(item['Date']);
  } else {
    result[name] = [item['Date']];
  }
})
Object.keys(result).forEach(function(item) {
  result[item] = Math.max.apply(null, result[item]);
})

将名称按名称添加到{Name: 'Temp', Date: ['2014-10-23', '2013-10-32', 'xxxx']} 然后通过Math.max.apply获取最大日期

答案 2 :(得分:0)

var array = [{"Name":"Temp","Date":new Date("2014-10-23")},
{"Name":"Temp","Date":new Date("2014-10-22")},
{"Name":"Temp","Date":new Date("2014-10-18")},
{"Name":"Temp","Date":new Date("2014-10-19")},
{"Name":"Temp2","Date":new Date("2014-10-12")},
{"Name":"Temp2","Date":new Date("2014-06-12")}];

var obj = {};
for(var i in array){
    if(!obj[array[i].Name] || obj[array[i].Name].getTime() < array[i].Date.getTime())
        obj[array[i].Name] = array[i].Date;
}

然后如果你需要它是一个对象数组:

array = [];
for(var name in obj){
    array.push({Name: name, Date: obj[name]});
}

答案 3 :(得分:0)

在这里,适用于任何随机订单

var Array = [{"Name":"Temp","Date":"2014-10-03"},
{"Name":"Temp","Date":"2014-10-22"},
{"Name":"Temp","Date":"2014-10-18"},
{"Name":"Temp","Date":"2014-10-19"},
{"Name":"Temp2","Date":"2014-10-12"},
{"Name":"Temp2","Date":"2014-06-12"}];

var tempArray = []; //This array will hold your Result Set

var tempArrayName = [];

var tempDate = {};

$.each(Array, function(i, v) {

  if(tempArrayName.indexOf(Array[i].Name) < 0){
    tempArray.push({ "Name":Array[i].Name,"Date":Array[i].Date});
    tempArrayName.push(Array[i].Name);
    tempDate[Array[i].Name] = Array[i].Date;
  }else{
   if( new Date((Array[i].Date))> new Date(tempDate[Array[i].Name])){

      $.each(tempArray, function(j, k) {
        if(tempArray[j].Name == Array[i].Name){
          tempArray[j].Date = Array[i].Date;
        }
      });
    }   
  }

});


console.log(tempArray);

答案 4 :(得分:0)

我建议不要覆盖Array对象。无论如何, 一种方法是使用对象对日期进行分组:

var data = [
    {"Name":"Temp","Date": new Date("2014-10-23")},
    {"Name":"Temp","Date": new Date("2014-10-22")},
    {"Name":"Temp","Date": new Date("2014-10-18")},
    {"Name":"Temp","Date": new Date("2014-10-19")},
    {"Name":"Temp2","Date": new Date("2014-10-12")},
    {"Name":"Temp2","Date": new Date("2014-06-12")}
];

var name2dates = Object.create(null);
data.forEach(function(thing){
    if(!name2dates[thing.Name]){
        name2dates[thing.Name] = thing.Date;
    }else{
        name2dates[thing.Name] = thing.Date > name2dates[thing.Name] ?
            thing.Date : name2dates[thing.Name];
    }
});

要从中创建一个'过滤'数组,如果这就是你的意思 需要,然后:

Object.keys(name2dates).map(function(name){
    return {Name: name, Date: name2dates[name]};
});

答案 5 :(得分:-1)

这有用吗?

$.grep(Array, function(v) {
        return v.Name === "Temp";
    }).reduce(function (a, b) { return a.Date > b.Date ? a : b; });