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