1

I have this array in my data.js file. And I want to find the specific date from other html file to data.js file using javascript.

For example:

Here is the date to search

var searchDate = '12-25-2017';

I want to search that date from data.js file ( Variable name : jpHolidays )and show in html or console.log().

I was googling and i didn't find how to find the specific date. Please help me.

var jpHolidays = {
     '01-01-2017' : '<span>お正月 <br> New Year\'s Day</span>',
     '01-02-2017' : '<span>お正月 <br> New Year\'s Day observed</span>',
     '01-09-2017' : '<span>成人の日 <br> Coming of Age Day</span>',
     '02-11-2017' : '<span>建国記念日 <br> National Foundation Day</span>',
     '03-20-2017' : '<span>春分の日 <br> Vernal Equinox Day</span>',
     '04-29-2017' : '<span>昭和の日 <br> Shōwa Day</span>',
     '05-03-2017' : '<span>憲法記念日 <br> Constitution Memorial Day</span>',
     '05-04-2017' : '<span>みどりの日 <br> Greenery Day</span>',
     '05-05-2017' : '<span>こどもの日 <br> Children\'s Day</span>',
     '07-17-2017' : '<span>海の日 <br> Marine Day / Ocean Day</span>',
     '08-11-2017' : '<span>山の日 <br> Mountain Day</span>',
     '09-18-2017' : '<span>敬老の日 <br> Respect for the Aged Day</span>',
     '09-23-2017' : '<span>秋分の日 <br> Autumnal Equinox Day</span>',
     '10-09-2017' : '<span>体育の日 <br> Health and Sports Day </span>',
     '11-03-2017' : '<span>文化の日 <br> Culture Day</span>',
     '11-23-2017' : '<span>勤労感謝の日 <br> Labour Thanksgiving Day </span>',
     '12-23-2017' : '<span>天皇誕生日 <br> The Emperor\'s Birthday</span>',
 };
Kyaw Zin Wai
  • 449
  • 5
  • 10
  • 26

6 Answers6

2

This is not an array. This is a JS object. Here is how you can iterate the properties of an object

for (var day in jpHolidays) {

  if (jpHoldays.hasOwnProperty(day) && day === searchDate) {
     console.log(jpHolidays[day]);  
  } 

}

It explains as - the for iterator takes you through all the properties of jpHolidays. You try to compare each property with your search string. The hasOwnProperty is to only consider the properties of the object - not the properties which it inherits prototypically.

Also, you can access the jpHolidays like jpHolidays[searchDate] as a direct approach.


This is a JS array a = [1, 2, 3];

This is an object a = {one: 1, two: 2, three: 3};

JS doesn't have associative arrays.

Charlie
  • 22,886
  • 11
  • 59
  • 90
1

A) Declare it as a global variable.

window.jpHolidays = {
     '01-01-2017' : '<span>お正月 <br> New Year\'s Day</span>',
     '01-02-2017' : '<span>お正月 <br> New Year\'s Day observed</span>',
     ...
};

B) Load data.js file before the file that you will use.
So the global variable, jpHolidays, can be stored in window. Like below:

<script src="data.js"></script>
<script src="yourFile.js"></script>

C) Grab datas from window.jpHolidays

console.log(jpHolidays['01-01-2017']);
moon
  • 640
  • 6
  • 14
1

First off, jpHolidays is an object and not an array.

var jpHolidays = {
     '01-01-2017' : '<span>お正月 <br> New Year\'s Day</span>',
     '01-02-2017' : '<span>お正月 <br> New Year\'s Day observed</span>',
     '01-09-2017' : '<span>成人の日 <br> Coming of Age Day</span>',
     '02-11-2017' : '<span>建国記念日 <br> National Foundation Day</span>',
.....
....
}


//assume that this is the holiday you are looking for
var isHoliday = '12-25-2017';

// access the jpHolidaysObject. if found, return the value. If not, print 'not holiday' or whatever suits you.

console.log(jpHolidays[isHoliday] || 'not holiday');
blueren
  • 2,730
  • 4
  • 30
  • 47
1

You can use the hasOwnProperty method to check for a key.

var searchDate = '12-25-2017';

if(jpHolidays.hasOwnProperty(searchDate)){
  //write your logic here or return jpHolidays[searchDate];
}
Neeraj Goswami
  • 101
  • 2
  • 6
0

jpHolidays is not an Array but an Object. You can access a particular value in that object by providing a key:

var result = jpHolidays[searchDate]

If you want to check wether a specific key exists at all in the object use the 'in' operator:

if (searchDate in jpHolidays) { // ...
Martin Schneider
  • 3,268
  • 4
  • 19
  • 29
0

You can simply access the object (not array) directly:

var jpHolidays = {
     '01-01-2017' : '<span>お正月 <br> New Year\'s Day</span>',
     '01-02-2017' : '<span>お正月 <br> New Year\'s Day observed</span>',
     '01-09-2017' : '<span>成人の日 <br> Coming of Age Day</span>',
     '02-11-2017' : '<span>建国記念日 <br> National Foundation Day</span>',
     '03-20-2017' : '<span>春分の日 <br> Vernal Equinox Day</span>',
     '04-29-2017' : '<span>昭和の日 <br> Shōwa Day</span>',
     '05-03-2017' : '<span>憲法記念日 <br> Constitution Memorial Day</span>',
     '05-04-2017' : '<span>みどりの日 <br> Greenery Day</span>',
     '05-05-2017' : '<span>こどもの日 <br> Children\'s Day</span>',
     '07-17-2017' : '<span>海の日 <br> Marine Day / Ocean Day</span>',
     '08-11-2017' : '<span>山の日 <br> Mountain Day</span>',
     '09-18-2017' : '<span>敬老の日 <br> Respect for the Aged Day</span>',
     '09-23-2017' : '<span>秋分の日 <br> Autumnal Equinox Day</span>',
     '10-09-2017' : '<span>体育の日 <br> Health and Sports Day </span>',
     '11-03-2017' : '<span>文化の日 <br> Culture Day</span>',
     '11-23-2017' : '<span>勤労感謝の日 <br> Labour Thanksgiving Day </span>',
     '12-23-2017' : '<span>天皇誕生日 <br> The Emperor\'s Birthday</span>',
 };
searchDate = '12-23-2017';
document.write(jpHolidays[searchDate]);
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76