Trying to match the year in a string in javascript

Amen Ra Source

I want to be able to take an array of strings:

Here is the mock data that I am using.

const archives = {
    "data": [{
            "id": 1,
            "type": "archive",
            "attributes": {
                "name": "Employee Engagement Summary",
                "description": "Report description goes here",
                "creation_date": "March 21, 2018",
                "date_range": "03/01/2018-05/15/2018",
                "data_sets": "Facility A, Nursing Department"
            }
        },
        {
            "id": 1,
            "type": "archive",
            "attributes": {
                "name": "Sample Survey 1",
                "description": "Report description goes here",
                "creation_date": "March 21, 2017",
                "date_range": "03/01/2017-05/15/2017",
                "data_sets": "Facility A, Nursing Department"
            }
        },
        {
            "id": 1,
            "type": "archive",
            "attributes": {
                "name": "Sample Survey 2",
                "description": "Report description goes here",
                "creation_date": "March 21, 2016",
                "date_range": "03/01/2016-05/15/2016",
                "data_sets": "Facility A, Nursing Department"
            }
        },
        {
            "id": 1,
            "type": "archive",
            "attributes": {
                "name": "Sample Survey 3",
                "description": "Report description goes here",
                "creation_date": "March 21, 2015",
                "date_range": "03/01/2015-05/15/2015",
                "data_sets": "Facility A, Nursing Department"
            }
        }
    ]
};

export default archives;

the above code is having the real mock data that i am using.

And just extract the year out of it. Because I want to be able to iterate through the years and place them on a list

<ul>
  <li>2018</li>
  <li>2017</li>
  <li>2016</li>
  <li>2015</li>
  <li>2014</li>
  <li>2013</li>
  <li>2012</li>
</ul>

javascripthtmljsonregex

Answers

answered 2 months ago Ryan Wilson #1

var str = [
  "March 21, 2018",
  "March 21, 2017f",
  "March 21, 2016",
  "March 21, 2015",
  "March 21, 2014",
  "March 21, 2013",
  "March 21, 2012"
]; 

//Array to hold the years
let years = [];

//Iterate your date collection
for(let i = 0; i < str.length; i++){
    //Split the string on the comma
    const splitter = str[i].split(",");

    //If the split produces more than one string value, meaning we have a formatted date
    if(splitter.length > 1){
       //Replace any non digit characters and trim whitespace
       let year = splitter[1].replace(/\D/g,'').trim();
       //push year value into new array
       years.push(year);

    }
}

//View the results in console
console.log(years);

answered 2 months ago pmkro #2

You can split on the , if your strings are always in the same format. I used the regex /[^0-9]/gi to replace anything that is not a number in the year with a non-character ''. Which prevents things like 2017f from including the f.

var dates = [
  "March 21, 2018",
  "March 21, 2017f",
  "March 21, 2016",
  "March 21, 2015",
  "March 21, 2014",
  "March 21, 2013",
  "March 21, 2012"
]; 

const years = [];
for(const date of dates){
  const year = date.split(',')[1].replace(/[^0-9]/gi, '').trim();
  years.push(year);
}

console.log(years);

answered 2 months ago colorgreen #3

Just match on four digits, at least for your case

(str => str.match(/\d{4}/)[0])

var dateStrings = [
  "March 21, 2018",
  "March 21, 2017f",
  "March 21, 2016",
  "March 21, 2015",
  "March 21, 2014",
  "March 21, 2013",
  "March 21, 2012"
];
var years = dateStrings.map(str => str.match(/\d{4}/)[0]);
var ul = years.reduce((ul, year) => {
  var li = document.createElement('li');
  li.innerHTML = year;
  ul.appendChild(li);
  return ul;
}, document.createElement('ul'));

document.body.appendChild(ul);

answered 2 months ago Get Off My Lawn #4

Use matches and check for 4 digits: /\d{4}/

var str = [
  "March 21, 2018",
  "March 21, 2017f",
  "March 21, 2016",
  "March 21, 2015",
  "March 21, 2014",
  "March 21, 2013",
  "March 21, 2012"
];

let ul = document.querySelector('ul')

str.forEach(date => {
  let matches = date.match(/\d{4}/)
  matches.length > 0 ? (ul.innerHTML += `<li>${matches[0]}</li>`) : null
})
<ul></ul>

answered 2 months ago Rohit Jindal #5

Try this :

var archives = {
    "data": [{
            "id": 1,
            "type": "archive",
            "attributes": {
                "name": "Employee Engagement Summary",
                "description": "Report description goes here",
                "creation_date": "March 21, 2018",
                "date_range": "03/01/2018-05/15/2018",
                "data_sets": "Facility A, Nursing Department"
            }
        },
        {
            "id": 1,
            "type": "archive",
            "attributes": {
                "name": "Sample Survey 1",
                "description": "Report description goes here",
                "creation_date": "March 21, 2017",
                "date_range": "03/01/2017-05/15/2017",
                "data_sets": "Facility A, Nursing Department"
            }
        },
        {
            "id": 1,
            "type": "archive",
            "attributes": {
                "name": "Sample Survey 2",
                "description": "Report description goes here",
                "creation_date": "March 21, 2016",
                "date_range": "03/01/2016-05/15/2016",
                "data_sets": "Facility A, Nursing Department"
            }
        },
        {
            "id": 1,
            "type": "archive",
            "attributes": {
                "name": "Sample Survey 3",
                "description": "Report description goes here",
                "creation_date": "March 21, 2015",
                "date_range": "03/01/2015-05/15/2015",
                "data_sets": "Facility A, Nursing Department"
            }
        }
    ]
};

var yearsRange = archives.data.map(obj => obj.attributes.date_range);

var yearArr= [];

yearsRange.filter(item => {
   yearArr.push(item.split('/')[2].split('-')[0]);
});

console.log("yearArr", yearArr);

comments powered by Disqus