返回数组和子节点 [英] Returning Arrays and ChildNodes
问题描述
我有以下的code:
var actionsAllowed = $(packet).find('actionsAllowed').get();
var actionArray = $(actionsAllowed).each(function () {
var actionNodes = this.childNodes;
var actionNumber = actionNodes.length;
var array = new Array(actionNumber)
for (var i = 0; i < actionNodes.length; i++) {
var action = actionNodes[i].nodeName
array[i] = action
console.log(action);
}
return array;
});
本搜索actionsAllowed
包(XML),并将其作为[actionsAllowed]
。
This searches the packet (XML) for "actionsAllowed"
and returns it as "[actionsAllowed]"
.
我然后试图创建与每个阵列中列出的操作的阵列
I am then trying to create an array with each of the actions listed in the array.
的本
变成actionsAllowed
没有[]
并允许它在形式的节点列表[ActionOne,ActionTwo,ActionThree]返回的子节点。
然后我得到的节点列表的长度和创建长度的数组。
The "this"
becomes "actionsAllowed"
without the "[ ]"
and that allows it to return the child nodes in the form "NodeList[ActionOne, ActionTwo, ActionThree]".
I then get the length of the NodeList and create an array of that length.
然后我遍历节点列表,每个添加到阵列中。
I then iterate over the NodeList and add each to the array.
截至去年底,它返回数组[ActionOne,ActionTwo,ActionThree]
,这是伟大的!
By the end, it returns the array as "[ActionOne, ActionTwo, ActionThree]"
, which is great!
但是 - 这就是问题所在:
BUT - this is the problem:
变量actionArray
变为的对象[actionsAllowed]
,而非阵列。
任何想法,这是为什么吗?我有一个理论,但我无法修复它=(
Any idea why this is please? I have a theory but I'm unable to fix it =(
感谢您!
推荐答案
$(actionsAllowed)。每个
返回迭代的第一要素。你似乎想这样的:
$(actionsAllowed).each
returns the first element of the iteration. You seem to want this :
var actionArray = [];
$(actionsAllowed).each(function () {
var actionNodes = this.childNodes;
var actionNumber = actionNodes.length;
var array = new Array(actionNumber)
for (var i = 0; i < actionNodes.length; i++) {
var action = actionNodes[i].nodeName
array[i] = action
console.log(action);
}
actionArray.push(array);
});
编辑:如果你想要的是一个很大的数组,而不是数组的数组,将其更改为
EDIT : If what you want is a big array instead of an array of arrays, change it to
var actionArray = [];
$(actionsAllowed).each(function () {
var actionNodes = this.childNodes;
for (var i = 0; i < actionNodes.length; i++) {
var action = actionNodes[i].nodeName
actionArray.push(action);
console.log(action);
}
});
这篇关于返回数组和子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!