简体   繁体   English

如何对从 AJAX 调用返回的数据进行分组?

[英]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.

相关问题 我如何从ajax调用中获取返回的数据 - how to do i get returned data from ajax call 如何使用普通 JavaScript 访问 AJAX 调用中返回的数据? - How do I access the data returned in an AJAX call with vanilla JavaScript? 如何访问从 MVC 视图中的 ajax 调用调用的控制器返回的数据 - How do I access data returned from a controller invoked by an ajax call in an MVC View 如何从ajax过滤返回数据的正文html? - How do I filter body html the returned data from ajax? 如何访问从 AJAX 请求返回的 JSON 数据? - How do I access JSON data returned from an AJAX request? 如何像数组一样使用索引访问从Ajax post方法调用返回的数据 - How to access data returned from Ajax post method call with index just like an array 在我的情况下,如何在 AJAX 回调 function 之外访问从 ajax 调用返回的数据? - How can I access data returned from ajax call outside AJAX callback function in my case? 在HighCharts中,我无法显示从ajax调用返回的数据 - In HighCharts, I Can't display data returned from ajax call 如何从jQuery中从Ajax调用返回的数据中获取对象? - How to get object from data returned from Ajax call in jQuery? 如何下载服务器在 Ajax 调用中返回的文件? - How do I download a file that is returned by the server in an Ajax call?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM