最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Accessing Javascript Array without using [] Brackets - Stack Overflow

programmeradmin3浏览0评论

I'm trying to write some javascript in an environment where square brackets are used for macro replacement, so they can't be used as normal in the script.

If I create an array as an object with new Array() I can use push() and pop() to access the elements, but for native arrays I can't figure out a way to get to the elements without using brackets. For example, the array returned from:

var allElements = document.getElementsByTagName("*");

Is there a way to assign a native array into an Array object so I can use push() and pop(), or is there another way to get inside?

I'm trying to write some javascript in an environment where square brackets are used for macro replacement, so they can't be used as normal in the script.

If I create an array as an object with new Array() I can use push() and pop() to access the elements, but for native arrays I can't figure out a way to get to the elements without using brackets. For example, the array returned from:

var allElements = document.getElementsByTagName("*");

Is there a way to assign a native array into an Array object so I can use push() and pop(), or is there another way to get inside?

Share Improve this question asked Dec 15, 2009 at 19:04 ovinophileovinophile 7931 gold badge9 silver badges20 bronze badges 3
  • 4 Is there no way of escaping the square brackets from the macro environment? That seems like the easiest way to go about it. – DMI Commented Dec 15, 2009 at 19:07
  • 1 Where are these marco replacements going to be? Will the be inside of externally included javascript files? – Zoidberg Commented Dec 15, 2009 at 19:07
  • This for an HTML report generator that really wasn't designed for hacking in this Javascript. I also just found that it's replacing | with ~ in my RegExp's... sigh. Thanks for the help everyone! – ovinophile Commented Dec 15, 2009 at 19:42
Add a ment  | 

6 Answers 6

Reset to default 6

yes, you can use prototype and slice method for example (Does not work in IE):

var index = 1;
Array.prototype.slice.call(allElements,index,index+1);

For IE, the only way I can think of is to copy all elements from collection to the array:

var newarr = new Array();
for(var i=0;i<allElements.length;i++){
    newarr.push(allElements[i]);
}

Or, you can use this function (Works in IE and Firefox):

window.atIndex = function(array,index){
    return eval("array" + String.fromCharCode(91) + String(index) + String.fromCharCode(93));
}

Get any item by using atIndex(allElements,0);

For NodeList collections you can use .item():

var allElements = document.getElementsByTagName("*");
var firstItem = allElements.item(0);

Source: NodeList

arr = [1, 2, 3]; //just a dummy array, I assume you don't use square brackets to assign an array in your code
el0 = arr.slice(0,1).pop(); //returns 0th element
el1 = arr.slice(1,2).pop(); //returns 1st element
...

Don't have IE here, but it works fine in Firefox.

First of all, the JavaScript engine will convert literals to objects for you (it does this behind the scenes)

var arr = [1,2,3];
alert( arr.pop() );

Secondly, understand that the return value from document.getElementsByTagName() is not an array. It's an HTMLCollection.

You could define a macro that inserts the [] braces. This might be possible. :-)

However, I am wondering why you still use this environment - even Notepad might be more fortable then.

for loops you can iterate through the collection like
for(var item in array){
doStuff(item);
}

You could also use Jquery's makeArray function
var newArray = $.makeArray(array);

I'm assuming you have no choice in your environment square braces are a normal thing in tons of programming languages. The ideal solution is use an environment that doesn't limit your programming

发布评论

评论列表(0)

  1. 暂无评论