0

I’m trying to grab all the links from a webpage, sort those links, find links using a key word, and print links with key words in a list to the console. I am using the split() method to find links with the key word, however, while my hard-coded string will split correctly, the same code will not work when I try splitting a value from an array.

I have an array of URLS (arrCopy), and I want to use split() on the first array value. However, I can’t get it to work using the code below.

//arrCopy is an array of strings (URLs)
var tempStringArr = arrCopy[0]; //get first URL from arr
var tempStringSplit = tempStringArr.split("/"); //doesn't work!

If I hard-code a URL into a string, that same code works.

//this code works
var exampleString = "http://bit.do/contact.php?layout=js.do"; //hardcoded string
var exampleStringSplit = exampleString.split("/");   //array created from exampleString

My full code which demonstrates the above examples: http://js.do/gloss/90965

I’ve commented out the code that doesn’t work.

How can I get the split() method to work on array values? Am I just using split() incorrectly?

4
  • 1
    Please put the code here, not at a remote link. We need to see what the elements of arrCopy are. It's obviously not an array of URLs as you say, the answers say that it's an array of DOM elements. Commented Apr 14, 2016 at 3:18
  • @Barmar Yes, I understand what went wrong now (I guess my fundamentals are a little lacking). I'm hesitant to post the full code with the question, however, because it's rather lengthy. Commented Apr 14, 2016 at 3:49
  • You should post a MCVE. Commented Apr 14, 2016 at 3:52
  • @Barmar Incidentally, that was what I was trying to do -- that was trimmed version of a much larger program (~1k lines). I didn't know what was wrong with how I used the array, so I kept everything that manipulated it. Commented Apr 14, 2016 at 4:06

5 Answers 5

3

The problem is that you're looping through an array of elements, then adding those elements in an array.

So, at the point you're attempting to do var tempStringArr = arrCopy[0]; you're actually grabbing the anchor(a) element.

So, to make your code work, you can do it like this:

var tempStringArr = arrCopy[0].href; //Get the href from the element.

OR:

var tempStringSplit = tempStringArr.href.split("/"); //doesn't work! //added .href

http://js.do/gborbonus/gets-and-prints-sorted-list-of-links-then-splits-each-link

I've added a js.do so you can see it working now.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for explaining so well! I'm new to Javascript, and figured I had a gap in understanding that led to my confusion in using split(). I spent days trying to figure this out.
No problem, sites like this exist to help, and people like all those that answered are here to help.
1

It's because the array you are building in arrCopy is an array of elements. So when you are doing the split, you are trying to do it on an html element rather then a string. Try this:

var tempStringArr = arrCopy[0].href;

Comments

0

Did you try using the string function ? var tempStringSplit = String(arrCopy[0]).split("/");

Comments

0

parseHTMLForLinks returns array of anchor elements and you might need to get urls from the anchor elements, if you want the href attribute of links you can replace this snippet in printList and try:

for (var i=0, j=arr.length; i<j; i++) { //make copy of links from HTML DOM
    arrCopy[i] = arr[i].href;
}

If you want urls from innerHTML you could try this:

for (var i=0, j=arr.length; i<j; i++) { //make copy of links from HTML DOM
    arrCopy[i] = arr[i].innerHTML;
}

Comments

0

Convert it to a string before the split

// added the code below at line 53
var t = [];
alert(t.length)
t = arrCopy[0].toString().split("/")
alert(t.length); // alerts 4

Comments

Your Answer

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.