AS3中如何向数组添加元素?
如何在 ActionScript3 中向数组添加元素
如果我有一个数组:
var myArray:Array;
如何向该数组“myArray”添加元素,如下所示:
myArray[] = value;
我的第二个问题是:如何比较数组元素值中是否存在变量值?< /strong>
类似于 php 中的 in_array
函数
How can add element to array in ActionScript3
If i have an array:
var myArray:Array;
How can add element to this array "myArray", something like this:
myArray[] = value;
My second question is: How can compare if variable value exist in array element value?
Something like in_array
function in php
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1. 所有这些都是向数组添加项目的不同方式。
someArray.push(someValue);
:添加最后一个项目someArray.unshift(someValue);
:添加第一个项目someArray[index] = someValue;
:在某处设置项目someArray.splice(index, 0, someValue);
:在某处插入项目2. 检查数组中是否存在某个值。
if (someArray.indexOf(someValue) == -1) { /*值不存在*/ }
参考Adobe livedocs 上的 ActionScript 语言参考。
1. All of these are different ways of adding item to array.
someArray.push(someValue);
: add last itemsomeArray.unshift(someValue);
: add first itemsomeArray[index] = someValue;
: set item somewheresomeArray.splice(index, 0, someValue);
: insert item somewhere2. Checking if a value is present in array.
if (someArray.indexOf(someValue) == -1) { /*value is not present*/ }
Refer to ActionScript language reference on Adobe livedocs.
要回答这里的两个问题,您可以通过直接访问或通过 push() 方法添加到数组,如下所示:
或
另外正如 Nox 指出的,您也可以使用 splice 方法添加元素。此方法用于删除特定索引处的 N 个元素,但您也可以同时在同一索引处注入一个或多个元素。
对于有关如何检查值或在数组中比较它们的第二个问题,这里有一种方法:
To answer both your questions here, you can add to an array by direct access or by the push() method, like so:
or
Also as Nox noted, you can use the splice method as well to add in elements. This method is used to delete N amount of elements at a specific index, but you can also simultaneously inject one or more elements at the same index.
For your second question about how to check values or compare them in an array, here is one method: