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)

asked Jul 22, 2019 at 14:14
3
  • 1
    Well, strings1.endsWith('/') === true Commented Jul 22, 2019 at 14:19
  • You could get rid of the trailing slash or check the last string if it ends with slash Commented Jul 22, 2019 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. Commented Jul 22, 2019 at 14:35

6 Answers 6

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)

answered Jul 22, 2019 at 14:23
Sign up to request clarification or add additional context in comments.

Comments

0

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

answered Jul 22, 2019 at 14:16

Comments

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

answered Jul 22, 2019 at 14:21

3 Comments

Seems a bit like an overkill :)
feels a bit better to me than normalizing the urls to all have '/' or not
I Agree this is nice way of thinking tough Resource wise its making 4 arrays and 2 sets instead of 1 simple string manipulation or equalisation...
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)

answered Jul 22, 2019 at 14:22

Comments

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

answered Jul 22, 2019 at 14:33

Comments

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.

answered Jul 22, 2019 at 14:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.