0

I'm working on an application that requires dates to be fed from JS and check in a JSON table what the "Letter" of the day is.

How can I take the JS date string that I've created (yyyy,mm,dd), and find the value of the corresponding key?

I have tried taking the string, stringifying it, and pushing it through a JS function.

var d = new Date();

      var date = d.getDate();
      var month = d.getMonth() + 1; 
      var year = d.getFullYear();
      var dateStr = year + "/" + month + "/" + date;
      console.log(dateStr);
      dateJSON = JSON.stringify(dateStr)
      console.log(dateJSON)
      alert(testVar.dateJSON)
var testVar = { //In a JS file
"2019-11-06": "D",
"2019-11-08": "A_con" //continues for a very long time.....
}

For "2019-11-08" I would like to have variable "letterDay" equal "A_con".

My code thus far returns "undefined" when I pull "testVar.dateJSON"

jaw12346
  • 65
  • 4
  • An altogether far too common naming problem: JSON is a text format for storing data. Once you've parsed the data, it's no longer JSON, it's just an object. If you have `var testVar = ` near it, it was never JSON to start with; it's just JavaScript. – Heretic Monkey Nov 08 '19 at 19:52
  • That said, this seems like a duplicate of [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable), since you appear to be trying to get the value of a property via its name, and that name is stored in a variable. The fact that you've "misspelled" the name is somewhat immaterial at this point. – Heretic Monkey Nov 08 '19 at 19:53

3 Answers3

1

You formatted your dateStr variable the wrong way ("/" instead of "-").

var testVar = { //In a JS file
"2019-11-06": "D",
"2019-11-08": "A_con" //continues for a very long time.....
}

var d = new Date('November 06, 2019 23:15:30');
var day = d.getDate();
var month = d.getMonth() + 1; 
var year = d.getFullYear();
var dateStr = year + "-" + month + "-" + (day > 9 ? day : '0' + day);
console.log(testVar[dateStr]); // D
Andre Nuechter
  • 2,141
  • 11
  • 19
1

I think it is far simpler than this, your data keys are in format yyyy-mm-dd while you're dateStr is in the format yyyy/mm/dd here is a simple snippet

var testVar = { //In a JS file
  "2019-11-09": "D",
  "2019-11-08": "A_con" //continues for a very long time.....
}
var d = new Date();
var date = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();

// Your mistake was here, the separators were '/'
var dateStr = year + "-" + month + "-" + date;
console.log(dateStr);

// Get the value dynamically
// when you have a dynamic key use testVar[dateStr]
// testVar.dateStr will literally look for a key called "dateStr" it will not evaluate the value of dateStr
console.log(testVar[dateStr]);
Ali Elkhateeb
  • 3,413
  • 4
  • 21
  • 39
0

Have your tried using JSON.parse()?

So: JSON.parse(testVar); and then modify your code and variables from there.

King11
  • 1,239
  • 3
  • 9
  • 17