roman
roman

Reputation: 5210

Unique values in javascript hash

I need to push a hash into array only if it doesn't contain the hash that i want to add. For example i have an array of hashes :

var someArray = [
        {field_1 : "someValue_1", field_2 : "someValue_2"},
        {field_1 : "someValue_3", field_2 : "someValue_4"},
        {field_1 : "someValue_5", field_2 : "someValue_6"}
    ]

The value

{field_1 : "someValue_1", field_2 : "someValue_2"}

should not be pushed into array as it is already there, but the value

{field_1 : "someValue_7", field_2 : "someValue_8"}

should, as the array does't contain such value.

Is there any way to do it using jQuery?

Now i'm just using $.each loop and check if the array contains some hash. If it does i trigger a flag. But i don't like this solution.

Upvotes: 2

Views: 2302

Answers (3)

Kumsal Obuz
Kumsal Obuz

Reputation: 825

Alternatively, you can augment Array or define your prototype as the following :

Array.prototype.add = function(element){
    var itemExists = false;
    for(var i=0;i<this.length;i++){
        if(this[i]==element){
            itemExists = true;
            break;
        }
    }
    if(itemExists==false)
        return this.push(element);
}

So, using [].add(yourElement) will only add that element to your array if it doesn't already exist. Feel free to change the structure of the argument and if condition according to your needs.

Upvotes: 1

Umesh Patil
Umesh Patil

Reputation: 10705

  1. create custom prototype to check the presence of object in the array. Please check the prototype design below.
Array.prototype.contains=function(x){
 for(i in this){
   if((x.field_1==this[i].field_1)&&(x.field_2==this[i].field_2))
     return true;  
 }
 return false;
}
  1. Using above prototype, you can check if object present in an array.
 var someArray = [
        {field_1 : "someValue_1", field_2 : "someValue_2"},
        {field_1 : "someValue_3", field_2 : "someValue_4"},
        {field_1 : "someValue_5", field_2 : "someValue_6"}
    ];

var a1={field_1 : "someValue_1", field_2 : "someValue_2"};
var a2={field_1 : "someValue_7", field_2 : "someValue_8"};

if(!someArray.contains(a1))   someArray.push(obj);  // it will NOT BE pushed
if(!someArray.contains(a2))   someArray.push(obj); // it will be pushed

Upvotes: 2

gotqn
gotqn

Reputation: 43666

I have faced this issue a lot of times, and what I always do is to write a function that returns true if the element is already in given set of elements.

function IsElementInSet(Element,Set)
{
    var ElementIsInSet=false;

    for(var i=0;i<Set.length;i++)
    {
        if(Set.field_1==Element.field_1&&Set.field_2==Element.field_2)
        {
                ElementIsInSet=true;
                break;
        }
    }

    return ElementIsInSet;
}

This is how I do the things. Create function that do some checks and give me true if the element is already there.

There are other possible solutions to optimise the search if you are working with big arrays and feel the things working slow, but I doubt you will.

EDIT: Also, about using parallel structure for saving current values - you can find it very tricky to have ordinary array (one dimension) in which only one value is saved - in your case it will be Element.field_1+Element.field_2 (concatenate the strings). Then you can write other function that check if there is given value in this array. I think the two solution are the same and it is question about style, not speed.

Upvotes: 0

Related Questions