2

I have an array with date range between three years. How to get unique years from it? I have tried this, but it pushes all items in the new array.

let array = [
  { x: Mon Feb 01 2016 , y:  32 }
  { x: Mon Feb 01 2016 , y:  95 }
  { x: Mon Feb 01 2017 , y: 117 }
  { x: Mon Feb 01 2018 , y:  23 }
  { x: Mon Feb 01 2018 , y:  14 }
]

let sortedArray = []

for (let i = 0; i < array.length; i++) {
  const obj = dates[i]
  sortedArray.push(obj.x.getFullYear())

}
Taplar
  • 24,788
  • 4
  • 22
  • 35
Yerlan Yeszhanov
  • 2,149
  • 12
  • 37
  • 67
  • 1
    Possible duplicate of [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates) – treyBake Jan 03 '19 at 16:32
  • Use a `Set` instead of an array. – 4castle Jan 03 '19 at 16:33
  • 1
    @hygull The OP should be the one to fix this issue with their question. Without their feedback, we do not know that these are strings. They could actually be Date ( reinforced by them trying to do `x.getFullYear()`) objects they incorrectly copied. I've rolled back your edit. – Taplar Jan 03 '19 at 16:56
  • Okay `@Taplar`, thank you for your suggestion and editing the question. – hygull Jan 03 '19 at 16:58

5 Answers5

3

You could map the year and get from a Set the unique values.

If necessary add a sorting.

unique.sort((a, b) => a - b); // asc

var array = [{ x: 'Mon Feb 01 2016', y:  32 }, { x: 'Mon Feb 01 2016' , y:  95 }, { x: 'Mon Feb 01 2017' , y: 117 }, { x: 'Mon Feb 01 2018' , y:  23 }, { x: 'Mon Feb 01 2018' , y:  14 }],
    unique = Array.from(new Set(array.map(({ x }) => new Date(x).getFullYear())));

console.log(unique);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

You can map the items to grab the x field's 4th token (separated by spaces) and throw those values into a set. You can use the spread operator to convert the values into an array and then sort for safe measure.

let array = [
  { x: 'Mon Feb 01 2016' , y:  32 },
  { x: 'Mon Feb 01 2016' , y:  95 },
  { x: 'Mon Feb 01 2017' , y: 117 },
  { x: 'Mon Feb 01 2018' , y:  23 },
  { x: 'Mon Feb 01 2018' , y:  14 }
]
let uniqueYears = [...new Set(array.map(item => item.x.split(' ')[3]))].sort();

console.log(uniqueYears);
.as-console-wrapper { top:0; max-height: 100% !important; }
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
1

I think you have pasted the code in hurry. Never do that as this allows other developers to understand and answer quickly. I have edited it little bit and made it working as follows.

let array = [
    { x: "Mon Feb 01 2016" , y:  32 },
    { x: "Mon Feb 01 2016" , y:  95 },
    { x: "Mon Feb 01 2017" , y: 117 },
    { x: "Mon Feb 01 2018" , y:  23 },
    { x: "Mon Feb 01 2018" , y:  14 }
];

let sortedArray = [];

for (let i = 0; i < array.length; i++) {
    let obj = array[i];
    let year = (new Date(obj.x)).getFullYear();

    if(!(sortedArray.indexOf(year) > -1)) {
        sortedArray.push(year); // Pushing year which does not exist in array
    }
}

console.log(sortedArray); // [ 2016, 2017, 2018 ]
hygull
  • 8,464
  • 2
  • 43
  • 52
0

you can do this

let array=[

{x: 'Mon Feb 01 2016' , y: 32},
{x: 'Mon Feb 01 2016' , y: 95},
{x: 'Mon Feb 01 2017' , y: 117},
{x: 'Mon Feb 01 2018' , y: 23},
{x: 'Mon Feb 01 2018' , y: 14},
];

let sortedArray = [];
let unique = [];
 for (let i = 0; i < array.length; i++) {
  const obj = array[i];
  var date = new Date(obj.x);
  if(!unique[date.getFullYear()]){
   sortedArray.push(date.getFullYear());
   unique[date.getFullYear()] = true;
  }

}
Islam ElHakmi
  • 274
  • 2
  • 10
0

You can also use combination of map, filter and indexOf

var array = [{ x: 'Mon Feb 01 2016', y:  32 }, { x: 'Mon Feb 01 2016' , y:  95 }, { x: 'Mon Feb 01 2017' , y: 117 }, { x: 'Mon Feb 01 2018' , y:  23 }, { x: 'Mon Feb 01 2018' , y:  14 }]
var unique = array
          .map( yearDate => new Date(yearDate.x).getFullYear())
          .filter( (year, index, arr) => arr.indexOf(year) === index ) 
console.log(unique) // [2016, 2017, 2018]
Sahil Raj Thapa
  • 2,353
  • 3
  • 12
  • 23