0

var strings1 = ("https://www.letsgodeep.com/deep/deeper/deepest/").split('/')
var strings2 = ("https://www.letsgodeep.com/deep/deeper/deepest/bottom").split('/')
console.log(Object.keys(strings1).length)
console.log(Object.keys(strings2).length)

Both of these return 7. I'm assuming it's because string 2 doesn't end with '/' but that will usually be the case. I can't figure out an easy way to tell if I have string1 or string2. Everything up to and including 'deeper' is always the same value.

UPDATE: Thanks for the help. JD's answer was a good idea but hngr18's solution worked out for what I need. (double check that we're on the right path && if length is greater than 6 we're at the 'bottom')

if ((location.href).split('/')[3] == 'deep' && Array.from(new Set(location.href.split('/'))).length > 6)
Fr3nZy
  • 65
  • 1
  • 6
  • 1
    Well, `strings1.endsWith('/') === true` – Mr. Polywhirl Jul 22 '19 at 14:19
  • You could get rid of the trailing slash or check the last string if it ends with slash – Kashkashio Jul 22 '19 at 14:20
  • The fact that operations the arrays of the same length does not mean that the contains of these arrays are the same. AFAIU, for the first `strings1`, last string in the array will be empty, while for `strings2`, the las string will be `bottom`. – Dmitriy Popov Jul 22 '19 at 14:35

6 Answers6

1

It's because when using split if the last character is the same that you are splitting on, it returns an empty string and adds it to the array.

So, yes, it's correct. One way to fix this would be to filter your results to remove empty strings by testing if the value is falsy.

var string1 = ("https://www.letsgodeep.com/deep/deeper/deepest/").split('/')
var string2 = ("https://www.letsgodeep.com/deep/deeper/deepest/bottom").split('/')

alert(string1.filter(_=>_).length)
alert(string2.filter(_=>_).length)
zfrisch
  • 8,474
  • 1
  • 22
  • 34
0

You could normalize the strings. If there is a trailing / then remove it. Then your method would work well!

ktilcu
  • 3,032
  • 1
  • 17
  • 15
0

Checking to see if it ends with a slash is a straightforward approach. str.endsWith('/') should do it.

Did you know that 'a/b/'.split('/') returns [ 'a', 'b', '' ]? There's an empty string that occurs after the last /.

If the last element is the empty string, then you can conclude that your original string ended with a /. (or the original was just the empty string.)

By the way, there's a very popular way to remove the empties described here, if you need to remove them after testing to see if the last one is empty.

...which basically involves filtering out strings that are falsey. (The empty string '' is falsey.) For example, 'a/b/'.split('/').filter(x=>x) returns only [ 'a', 'b' ].

But be careful, because if you filter the empties, then you can't tell the difference between a/b and a//b.

Wyck
  • 10,311
  • 6
  • 39
  • 60
0

As the last item in the array strings2 is "" (empty string) and there is already a "" from the http://, use Array.from with a new set, these de-duplicate the data items and return a more faithful value

console.log(Array.from(new Set(strings1)).length);

returns 6

console.log(Array.from(new Set(strings2)).length);

returns 7

hngr18
  • 817
  • 10
  • 13
0

You can check if the last item of splitted array is empty or not.

if(string1[string1.length-1]!="")
   yourString = string2;
else
   yourString = string1;

Or you can remove the last (/) from string. then you can split and check.

var strings1 = "https://www.letsgodeep.com/deep/deeper/deepest/";
var strings2 = "https://www.letsgodeep.com/deep/deeper/deepest/bottom";
if (strings1.substring(strings1.length-1) == "/")
    strings1 = strings1.substring(0, strings1.length-1);
strings1 = strings1.split('/');
strings2 = strings2.split('/');
console.log(Object.keys(strings1).length)
console.log(Object.keys(strings2).length)
Jaydeep
  • 1,686
  • 1
  • 16
  • 29
0

I think you can use regular expression

console.log("https://www.letsgodeep.com/deep/deeper/deepest/".match(/\/\S/ig).length); // 4
console.log("https://www.letsgodeep.com/deep/deeper/deepest/bottom".match(/\/\S/ig).length); // 5

Hope this helps

Syam
  • 303
  • 1
  • 8