2

I created a list in sharepoint gathering user information. 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.

$.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
Pawan Tiwari
  • 518
  • 6
  • 26
  • Maybe a duplicated question of : [remove-duplicate-values-from-js-array](https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array) – Shim-Sao Apr 26 '19 at 11:44

3 Answers3

1

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

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);
Adrian
  • 8,271
  • 2
  • 26
  • 43
1

You can remove duplicate values using a Set and then converting it back to an array:

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"]```
0

var arr = ['3/3/2019', '3/3/2019', '4/4/2019']

arr.filter(function(item, i){ return arr.indexOf(item) == i;})

DDD
  • 471
  • 1
  • 5
  • 13