[英]How do I group like data returned from an AJAX call?
I created a list in sharepoint gathering user information.我在 sharepoint 收集用户信息中创建了一个列表。 Each entry has date.
每个条目都有日期。 Some days have many entries.
有些日子有很多条目。 My goal is to create a select menu that has the dates.
我的目标是创建一个包含日期的选择菜单。 When I click the menu the dates populate but they repeat.
当我单击菜单时,日期会填充但它们会重复。 How do I return them as a single value?
如何将它们作为单个值返回?
I've tried to nest another for loop and an if statement.我试图嵌套另一个 for 循环和一个 if 语句。
$.ajax({
url:url,
method:'GET',
headers:{ 'Accept': 'application/json; odata=verbose' },
success: function(data){
var dat = data.d.results;
for (var i = 0;i < dat.length; i++){
html+='<optionvalue='+itm.dateVal+'>'+itm.dateVal+'</option>';
} $('#week-selector').append(html);
},
error: function(e){
console.log('error:'+e);
}
});
The output I am getting is:我得到的输出是:
3/3/2019
3/3/2019
3/3/2019
3/3/2019
3/6/2019
3/6/2019
3/10/2019
3/10/2019
The output I want to get is:我想得到的输出是:
3/3/2019
3/6/2019
3/10/2019
You can easily write a filter function to get distinct values.您可以轻松编写过滤器函数来获取不同的值。
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);
in ES6 you can do it the following way在 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);
You can remove duplicate values using a Set and then converting it back to an array:您可以使用 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"]```
var arr = ['3/3/2019', '3/3/2019', '4/4/2019'] var arr = ['3/3/2019', '3/3/2019', '4/4/2019']
arr.filter(function(item, i){ return arr.indexOf(item) == i;}) arr.filter(function(item, i){ return arr.indexOf(item) == i;})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.