0

I have array with data :

["05:45", "06:00", "06:15", "06:30", "06:45", "07:00", "07:15", "07:30", "07:45", "08:00", "08:15", "08:30", "08:45", "09:00", "09:15", "09:30", "09:45", "10:00", "10:15", "10:30", "10:45", "11:00", "11:15", "11:30", "11:45", "12:00", "12:15", "12:30", "12:45", "13:00", "13:15", "13:30", "13:45", "14:00", "14:15", "14:30", "14:45", "15:00"]

For this data i need to replace numbers which are not equal to zero, so i must have array like this:

["", "06:00", "", "", "", "07:00", "", "", "", "08:00", "", "", "", "09:00", "", "", "", "10:00", "", "", "", "11:00", "", "", "", "12:00", "", "", "", "13:00", "", "", "", "14:00", "", "", "", "15:00"]

For this i create function but for some reason it's not working:

var intervals = ["05:45", "06:00", "06:15", "06:30", "06:45", "07:00", "07:15", "07:30", "07:45", "08:00", "08:15", "08:30", "08:45", "09:00", "09:15", "09:30", "09:45", "10:00", "10:15", "10:30", "10:45", "11:00", "11:15", "11:30", "11:45", "12:00", "12:15", "12:30", "12:45", "13:00", "13:15", "13:30", "13:45", "14:00", "14:15", "14:30", "14:45", "15:00"]

function hideNumbers() {
  for (var i = 0; i < intervals.length; i++) {
    var c = intervals[i].split(':');
    var time = parseInt(c[1])
    var result = (c[0] + ':' + c[1])

    if (time != 00) {
      intervals[i].replace(result, "");
    }
  }
  console.log(intervals)
}

hideNumbers();

So what is the problem?

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
Andrew
  • 572
  • 6
  • 29

1 Answers1

3

.replace() doesn't replace in-place, it returns the replacement as a new string, so you need:

intervals[i] = intervals[i].replace(result, "");

var intervals = ["05:45", "06:00", "06:15", "06:30", "06:45", "07:00", "07:15", "07:30", "07:45", "08:00", "08:15", "08:30", "08:45", "09:00", "09:15", "09:30", "09:45", "10:00", "10:15", "10:30", "10:45", "11:00", "11:15", "11:30", "11:45", "12:00", "12:15", "12:30", "12:45", "13:00", "13:15", "13:30", "13:45", "14:00", "14:15", "14:30", "14:45", "15:00"]

function hideNumbers() {
  for (var i = 0; i < intervals.length; i++) {
    var c = intervals[i].split(':');
    var time = parseInt(c[1])
    var result = (c[0] + ':' + c[1])

    if (time != 00) {
      intervals[i] = intervals[i].replace(result, "");
    }
  }
  console.log(intervals)
}

hideNumbers();

Extra:

As pointed out in the comments, as you are looking to remove that entry, there's no need for the .replace() at all.

But it's useful to know why x.replace(... doesn't change x and needs to be x = x.replace(....

As an other extra: there's also no need for the parseInt(c[1]) and you can compare directly with if (c[1] === "00")


Extra Extra:

While the question was about your code specifically (with .replace() not replacing), there are other ways to do this, eg:

var intervals = ["05:45", "06:00", "06:15", "06:30", "06:45", "07:00", "07:15", "07:30", "07:45", "08:00", "08:15", "08:30", "08:45", "09:00", "09:15", "09:30", "09:45", "10:00", "10:15", "10:30", "10:45", "11:00", "11:15", "11:30", "11:45", "12:00", "12:15", "12:30", "12:45", "13:00", "13:15", "13:30", "13:45", "14:00", "14:15", "14:30", "14:45", "15:00"]

function hideNumbers() {
  intervals = intervals.map(x => x.split(":")[1]!=="00" ? "" : x);
  console.log(intervals)
}

hideNumbers();
freedomn-m
  • 27,664
  • 8
  • 35
  • 57