0

I have the following javascript code

var list = [
{
  Date : '2014-12-31 12:23:43',
  DateStart: '1980-12-30 23:43:42',
  Name: 'Kate',
  ...
  ...
},
{
  Date : '1978-05-21 23:43:65',
  DateStart: '1973-04-06 12:34:09',
  Name: 'John',
  ...
  ...
}
];

And the following code to verify for regex pattern:

for (var i in list) {
     var data = [];
     if (/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/.test(list[i].?)) {
         data.push({Data: list[i].Data });
     }
}

The variable i from the above code is to interact with each line of the array.

How can I interact through for loops with each line and column without having to specify this in the question mark from the above code?

What can I do to all columns be verified for the date pattern?

Making the array date stay only with the values

2014-12-31 12:23:43,
1980-12-30 23:43:42,
1978-05-21 23:43:65,
1973-04-06 12:34:09
  • It's an array of objects. `column = list[5].Date` – Marc B Jan 08 '15 at 14:53
  • But I want that the `column` stay with the Data value instead 00/00/0000 – jean cavalcante Jan 08 '15 at 15:00
  • "stay"? huh? You want `column` to be more of a pointer to a specific entry in `list`? Not really possible. JS doesn't have pointers. – Marc B Jan 08 '15 at 15:01
  • Need a far better explanation of objectives. Please put more effort into your questions so that they are clear and concise. Also provide expected results – charlietfl Jan 08 '15 at 15:09
  • 1
    We need more detailed information on what you're trying to achieve. Do you want to extract everything that is a date from your object? What part of your code doesn't work? Can you come up with a jsfiddle to help us view your problem? – Marco Aurélio Deleu Jan 08 '15 at 15:53

2 Answers2

0

How can I interact through for loops with each line and column without having to specify this in the question mark from the above code?

You have to specify the field you want to access, how else should the interpreter be able to know what you want to access?

If you want to avoid using an indexer you can use external libraries like underscore.js to iterate over the collection:

http://underscorejs.org/#each

_.each(list, function(item) { 
  // validate item.Date and item.DateStart here
});

Alternatively there is a very concise post about this topic here: For-each over an array in JavaScript?

Community
  • 1
  • 1
ovm
  • 2,452
  • 3
  • 28
  • 51
  • I put `_.each(list, function(item) { var data = []; if (/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/.test(item.Data)) { data.push({ Data: item.Data }); } });`, but it only verifies the specified column. I want to verify column by column, line by line, to verify if the pattern matches. Like when you are going to verify a table `for(line=0;line – jean cavalcante Jan 08 '15 at 17:47
0

Was this what I wanted

for (var j = 0; j < list.length; j++) {
        for (p in list[j]) {
            if (/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/.test(list[j][p])) {
                data.push({Data: list[j][p] });
            }
        }
    }