English 中文(简体)
发挥功能 Java s ArrayList.remove(o)
原标题:Function to imitate Java s ArrayList.remove(o)

我如何履行与<代码”相同的职能。 ArrayList.remove(o)?

我最接近的是

Array.prototype.remove = function(o) {
    var index = this.indexOf(o);
    if(index == -1) {
        return;
    }
    return this.splice(index, index);
}

但它在第二行投下了一个错误,声称有<编码>indexOf(o)。 (cannot found function indexOf()

最佳回答

Array.prototype.indexOf is not support by IE7 and below. 因此,您需要shim<>。 这种方法像以前一样盛行。

Array.prototype.indexOf = Array.prototype.indexOf || function( search ) {
    for(var i = 0, len = this.length; i < len; i++) {
        if( this[ i ] === search ) {
            return i;
        }
    }
    return -1;
};

<>(简单例子)。

此外,你要求<代码>.splice(>)的说法是错误的,第二点是你想要删除的内容的长度,应当改为1

Array.prototype.remove = function(o) {
    var index = this.indexOf(o);
    if(~index) {
        this.splice(index, 1);
    }
};
问题回答

另一种想法是使用和“联系阵列”,一种带有名称价值的字面物体,并使用在所有现代浏览器中工作的删除操作者。

var arr = { x : somval,  y : someval2 ...}
delete arr.x;




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...