Reputation: 7112
I'm trying to create a 2D array consisting of the URL and text in an "a href" element.
I think I got it working, but I am not sure if this is the correct structure for a 2D array. From the output in the console, it looks like a 1D array.
I need it to be like this [link URL, linkText]
example: [http://www.yahoo.com, Yahoo!]
Could someone please take a look and see if this is actually a 2D array?
Thanks
Here is my jsfiddle:
var linkArray = [];
$('[class=hrefURL]').each(function (i) {
var text = ($(this).text());
var url = ($(this).attr('href'));
linkArray.push(url, text);
});
for (var i = 0; i < linkArray.length; i++) {
console.log(linkArray[i]);
}
Upvotes: 0
Views: 88
Reputation: 1245
Every item you push its at the same level. Try this:
var linkArray = [];
$('[class=hrefURL]').each(function (i) {
var text = ($(this).text());
var url = ($(this).attr('href'));
linkArray.push([url, text]);
});
for (var i = 0; i < linkArray.length; i++) {
console.log(linkArray[i]);
}
Upvotes: 3
Reputation: 359966
Change
linkArray.push(url, text);
to
linkArray.push([url, text]);
or use objects instead of arrays as the inner element for something a bit more structured:
linkArray.push({url: url, text: text});
Upvotes: 4