0

For example I have code like this:

var myArr="14:44:45".split(":");

But sometimes time value doesn't have "seconds" information.

var myArr="14:44".split(":");

Basicly sometimes the string has value of "seconds" and sometimes has not.So how can I check if the myArr[2] has value.What is the best practice for that?

if(myArr.length === 3) 

Is it okay?

Edit: or if(myArr[2])

2
  • what would you do after checking? Commented Feb 18, 2018 at 20:03
  • Yeap, it's the most effective way to know how many items are in the myArr array.
    – Christos
    Commented Feb 18, 2018 at 20:04

3 Answers 3

3

Your approach is correct

A better approach is destructuring the array and checking for the variable seconds

let [h, m, s] = "14:44:45".split(":");
if (s) console.log('Has seconds');

let [hh, mm, ss] = "14:44".split(":");
if (!ss) console.log("Doesn't have seconds");

Resource

1

As others have said, your approach is almost fine. You might want to check that the length is greater than 2 rather than equal to 3, but that might never be an issue.

But I have an alternate suggestion:

const timeParts = time => {
  const [hours, minutes = 0, seconds = 0] = time.split(':').map(Number)
  return {hours, minutes, seconds}
}

console.log(timeParts('14:44:45')) //=> {hours: 14, minutes: 44, seconds: 45}

console.log(timeParts('14:45')) //=> {hours: 14, minutes: 45, seconds: 0}

Rather than doing an explicit check this simply gives default values for the minutes and seconds. If you don't want the numeric conversion replace the first line of the function with

const [hours, minutes = '00', seconds = '00'] = time.split(':')
0

Example 1 uses split() and .length` and a ternary operator to compare length of array. If you only have one or two timestamps this is sufficient.

Example 2 uses .map() to iterate through an array of timestamps. Each timestamp is compared by the same function from the the previous example. If you have more than one timestamp to check try this example.

Example 3 is like the previous example and it will append :00 to any timestamp that needs it. If you have more than one timestamp and actually want to fix them as well try this example.

Demo

//~~~~~~~~~~~~If you have one or two timestamps~~~~~~~~~~~~~Example 1

function sec(stamp) { 
  return stamp.split(':').length < 3 ? 'No Seconds':'Has Seconds';
}

let r0 = sec("14:44:45");
let r1 = sec("14:44");

console.log(r0);
console.log(r1);

//~~~~~~~~~~~~If you have more than one timestamp~~~~~~~~~~~~Example 2

const times = ["14:44:45", "14:44", "20:59:10", "7:23:32", "3:04", "15:46", "8:18"];

const secChk = times.map(function(time) {
  return time.split(':').length < 3 ? 'No Seconds':'Has Seconds';
});

console.log(secChk);

//~~~~~If you have more than one timestamp you want to fix~~~~Example 3

const secFix = times.map(function(time, index) {
  return time.split(':').length < 3 ? '['+index+']: '+time+':00 - Fixed':'['+index+']: '+time;
});
  
console.log(secFix);

4
  • "overly complex": yes! What's the point of taking the question which asks about parsing one string and giving an answer that formats a list of them? Commented Feb 18, 2018 at 22:10
  • "So how can I check if the myArr[2] has value." So the objective is not this or this?: Check array elements has value
    – zer00ne
    Commented Feb 18, 2018 at 22:35
  • The OP wanted to check that the array resulting from timeStr.split(':') had a third element, representing seconds. Commented Feb 18, 2018 at 22:41
  • "The OP wanted to check that the array resulting from timeStr.split(':') had a third element, representing seconds." Yeah, see example 2 and 3. "What's the point of taking the question which asks about parsing one string and giving an answer that formats a list of them? " Because of the fact OP says sometimes the timestamp is like 11:11:11 and sometimes timestamp is like 11:11. That pattern along with the fact the object is a timestamp infers that there's a possibility that he could have an array. Besides Example 1 covers for a condition when one timestamp exists instead of array.
    – zer00ne
    Commented Mar 30, 2018 at 23:11

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.