如何过滤此数组
var arr = [[1, "3/20/2018", "A"], [1, "3/20/2018", "B"],
[1, "4/20/2018", "A"], [1, "4/20/2018", "B"],
[2, "1/30/2018", "A"], [2, "1/30/2018", "B"],
[2, "3/30/2018", "A"], [2, "3/30/2018", "B"]
];
我想过滤每个其他子元素(ID)的最新日期元素。要获得此输出:
var result = [[1, "4/20/2018", "A"], [1, "4/20/2018", "B"],
[2, "3/30/2018", "A"], [2, "3/30/2018", "B"]
];
答案 0 :(得分:0)
一些观察/假设
3/20/2018
附近缺少引号。没有引号,这只是一个浮点数基于以上所述,您可以尝试以下
var arr = [[1, "3/20/2018", "A"], [1, "3/20/2018", "B"], [1, "4/20/2018", "A"], [1, "4/20/2018", "B"], [2, "1/30/2018", "A"], [2, "1/30/2018", "B"], [2, "3/30/2018", "A"], [2, "3/30/2018", "B"]];
var obj = {};
arr.forEach(function(item){
var id = item[0] + "_" + item[2];
if(obj[id]) {
if(new Date(item[1]) > new Date(obj[id][1])) {
obj[id] = item;
}
} else {
obj[id] = item;
}
});
console.log(Object.values(obj));