Reputation: 2449
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())
}
Upvotes: 2
Views: 401
Reputation: 2483
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]
Upvotes: 0
Reputation: 8740
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 ]
Upvotes: 1
Reputation: 386746
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);
Upvotes: 3
Reputation: 274
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;
}
}
Upvotes: 0
Reputation: 48693
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; }
Upvotes: 1