3 回答
TA贡献1886条经验 获得超2个赞
您可以使用Set删除重复的值,然后将其转换回数组:
var arr = [
"3/3/2019",
"3/3/2019",
"3/3/2019",
"3/3/2019",
"3/6/2019",
"3/6/2019",
"3/10/2019",
"3/10/2019"
];
console.log(Array.from(new Set(arr)));
// ["3/3/2019", "3/6/2019", "3/10/2019"]```
TA贡献1817条经验 获得超6个赞
您可以轻松编写过滤器函数以获取不同的值。
var arr = [
"3/3/2019",
"3/3/2019",
"3/3/2019",
"3/3/2019",
"3/6/2019",
"3/6/2019",
"3/10/2019",
"3/10/2019"
];
var distinctDates = arr.filter(function(val, idx, self){ return self.indexOf(val) === idx; });
console.log(distinctDates);
在ES6中,您可以按以下方式进行操作
var arr = [
"3/3/2019",
"3/3/2019",
"3/3/2019",
"3/3/2019",
"3/6/2019",
"3/6/2019",
"3/10/2019",
"3/10/2019"
];
var distinctDates = [...new Set(arr)];
console.log(distinctDates);
TA贡献1829条经验 获得超13个赞
var arr = ['3/3/2019','3/3/2019','4/4/2019']
arr.filter(function(item,i){return arr.indexOf(item)== i;})
添加回答
举报