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>',
 };
4
  • 1
    What you have tried so far? Commented Dec 4, 2017 at 7:27
  • '12-25-2017' is not in your holidays object Commented Dec 4, 2017 at 7:28
  • Just FYI guys, jpHolidays is not an array. Its an object. Commented Dec 4, 2017 at 7:29
  • This is actually object, and you need to get coresponding key and value: stackoverflow.com/questions/8517089/js-search-in-object-values Commented Dec 4, 2017 at 7:29

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You no need to iterate through all the key to get a value right?
How can i find From Date To Date in that object array? please.
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');

1 Comment

Thanks you very much. I got it .
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']);

Comments

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];
}

Comments

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) { // ...

Comments

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]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.