-3

Any one with an idea on how i can access the "13-02-2018"? Help will be highly appreciated. the object I have named the array "Data"

for (var i = 0; i < Data.length;i++){
                var temp =Data[i].[0];
                    }
edwin walela
  • 48
  • 1
  • 5
  • What have you tried so far? Any more information on the scope of the work? – CodeMonkey123 Feb 27 '18 at 15:40
  • You'd have to search the array for that value – James Parsons Feb 27 '18 at 15:42
  • Do you have to dynamically access the property? ... Also do you have control of what the property is called? If possible you should consider changing the structure of your object. – Tyler Feb 27 '18 at 15:44
  • 1
    @ABear i have updated the question – edwin walela Feb 27 '18 at 15:46
  • I am receiving the data from an API, the dates vary. I was wondering if i can extract that value(which is the date) and store it in a variable @Tyler – edwin walela Feb 27 '18 at 15:49
  • Seeing as the property name is a date, I expect it to change. Which makes the situation quite complex to do in a very clean and simple way. As far as I know, JL's answer might be the best direction to go. – Tyler Feb 27 '18 at 16:01

2 Answers2

1

My solution would be to loop through the data, as you are doing, and check to see if the element is of a date format. I assume the dates are always of the same format (ie "dd-mm-yyyy)? If so I would check they match that format using regex (there are other ways to do this but I love regex!), so try looking at a thread like this: date format detection by regex - all credit to the original answers in the link. Beware you will need to update the accepted answer so you get "dd-mm-yyyy" (I'm not certain if the below will work out the box as I don't know how Date() works in JS, you may have to rearrange it to a "yyyy-mm-dd" format before using it).

But yeah I'd go for something like:

for (var i = 0; i<data.length; i++){
   for (var item in data[i]) {
       if(item.isValidDate()) {
           \\do your stuff, for example:
           var temp = item;
       }
   }
}

function isValidDate(dateString) {
   var regEx = /^\d{2}-\d{2}-\d{4}$/;
   if(!dateString.match(regEx)) return false;  // Invalid format
      var d = new Date(dateString);
   if(!d.getTime() && d.getTime() !== 0) return false; // Invalid date
      return d.toISOString().slice(0,10) === dateString;
}

Even if this doesn't work, hopefully it gives you an idea of a possible path you could go down :)

CodeMonkey123
  • 1,046
  • 13
  • 22
0

Your question is "how do I list the keys in an object?"

var foo = { bar: 'baz' };
for (var key in foo) { console.log(key); }
// outputs 'bar'

More information about keys: Checking if a key exists in a JavaScript object?

EDIT: OP updated his question and data.

var foo = [ { bar: 'baz' }, { buz: 'biz' } ];
for (var i = 0; i < foo.length; i++) {
    for (var key in foo[i]) {
        console.log(key);
    }
}
J L
  • 62
  • 1
  • 4
  • Your question should include a full description of your data that's neatly formatted. – J L Feb 27 '18 at 16:57