Home > database >  How to get the years from an array of dates in order
How to get the years from an array of dates in order

Time:09-29

I have this array:

var array1 = ['9/2022','7/2020','11/2021','12/2020','2/2020','4/2021','10/2021'];

How can I get the years from it in order? So the result would be this:

result = ['2020','2021','2022'];

Do I have to change the array to a new Date() format and sort, or how would be the best way ? Any Suggestion ?

CodePudding user response:

Using a combination of ... , Set , .map() and .sort(). This can be done in one line.

... -> destructures array into individual items.

new Set() -> make a set out of the new items.

map() -> runs a loop and map the array into a new one.

split() -> breaks a string into array.

sort() -> sorts an array.

var array1 = ['9/2022','7/2020','11/2021','12/2020','2/2020','4/2021','10/2021'];

console.log([...new Set(array1.map(x => x.split('/')[1]))].sort());

CodePudding user response:

You can easily achieve this result using Set and map

var array1 = [
  "9/2022",
  "7/2020",
  "11/2021",
  "12/2020",
  "2/2020",
  "4/2021",
  "10/2021",
];

const result = [...new Set(array1.map((s) => s.split("/")[1]))].sort();

console.log(result);

CodePudding user response:

You can map to get only the years, and then sort the results. Then you instantiate a Set which removes all duplicate entries.

new Set(['9/2022','7/2020','11/2021','12/2020','2/2020','4/2021','10/2021']
  .map(dateAsString => dateAsString.split('/')[1])
  .sort());

CodePudding user response:

Logic

  • Loop through array
  • Generate the year array by splitting the data on /.
  • Generate unique items using Array.reduce
  • Sort the array.

const array1 = ['9/2022', '7/2020', '11/2021', '12/2020', '2/2020', '4/2021', '10/2021'];
const result = array1.reduce((acc, curr) => {
  const year = curr.split('/')[1];
  if (!acc.includes(year)) {
    acc.push(year);
  }
  return acc;
}, []).sort();
console.log(result);

CodePudding user response:

First convert all of your dates to strings containing the year only. You can do this using map() on the array and the split() method on each string. Then convert the year strings into numbers by using the parseInt() function. Sort the year numbers using the default Array.sort() method and create a new Set (only containing unique values from the given array). Using the spread operator (...), convert that Set into an Array.

var array1 = ['9/2022','7/2020','11/2021','12/2020','2/2020','4/2021','10/2021'];

var years = [...new Set(array1.map(date => parseInt(date.split("/")[1])).sort())];

CodePudding user response:

One method is to iterate through the array split the string into month and year and return only the year: (~~ converts string into integer)

var array1 = ['9/2022','7/2020','11/2021','12/2020','2/2020','4/2021','10/2021'];

var years = [...new Set(array1.map(e => ~~e.split("/")[1]))];

console.log(years.sort())

CodePudding user response:

simply you can use map method to iterate over your array and then get every element and split them or separate them using Regex.

var array1 = ['9/2022','7/2020','11/2021','12/2020','2/2020','4/2021','10/2021'];

// separate each data into month and year and get the years in the result array
const result = array1.map(data => data.split("/")[1])

// make years uinque with this simple hack 
const uniqueResult = [...new Set(result)]

// now you can sort your the data 
const sortedUniqueResult = uniqueResult.sort((a, b) => a - b)

console.log(sortedUniqueResult)

  • Related