---
title: "jQuey List (unique)"
author: "chenzenghua"
date: "2012-01-09T10:09:59Z"
tags: []
categories: ["Web前端"]
slug: "jquey-list-unique"
draft: false
lastmod: "2012-01-09T10:09:59Z"
id: 1338457
url: "https://chenzenghua.iteye.com/blog/1338457"
---

```js

jQuery.uniqueArray = function () {
    this.items = new Array();
    this.itemsCount = 0;
    this.add = function (value) {
        if (!this.contains(value)) {
            this.items.push(value);
            this.itemsCount++;
        }
        else
            throw "The value '" + value + "' allready exists."
    }

    this.contains = function (value) {
        return jQuery.inArray(value, this.items) > -1;
    }

    this.clear = function () {
        this.items = new Array();
        this.itemsCount = 0;
    }
    this.size = function () {
        return this.itemsCount;
    }

    this.isEmpty = function () {
        return this.size() == 0;
    }

    this.remove = function (value) {
        if (this.contains(value)) {
            var index = jQuery.inArray(value, this.items);
            this.items.splice(index, 1);
            this.itemsCount--;
        }
        else
            throw "value '" + value + "' does not exists."
    }
};

```