0

Based on a post I've seen. I am creating a temporary array elements by clicking on a hyperlink. The only problem I have is that it can generate repeated elements, which until now have not been resolved:

var items = new Array();
$("ul.dropdown li a").click(function(e)
{
    e.preventDefault();

    var id      = $(this).attr('href');
    var name    = $(this).text();

    if(!$.findFromArray('id', id, items))
    {
        return false;       
    }
    else
    {
        items.push({
            "id"    : id,
            "name": name
        });

        //genate row
        var newRow = $("<tr itemId=\"" + id  + "\" />")
        .appendTo(".form_list.left tbody")
        .append("<td>" + name + "</td>")
        .append("<td><a href=\"#\" class=\"deleteRow\">delete</a></td>");


        $(".deleteRow", newRow).click(function(e) {
                e.preventDefault();
                items = $.removeFromArray('id', $(this).closest("tr").attr("itemId"), items);
                $(this).closest("tr").remove(); 
        });     
    }
});

I created a function "$.FindFromArray" that searches the array the insert and returns FALSE if there is to avoid inserting in the array and generate a line in the table:

$.findFromArray = function(property, value, arr)
{
    $.each(arr, function(elem, index)
    {
        if(elem[property] === value)
        {
            return false;           
        }   
    }); 
};

But this function always returns me FALSE.

As I can avoid duplication before inserting the element? if a better way to do it ... I hope I can help.

1

2 Answers 2

3

Change you method to

$.findFromArray = function(property, value, arr)
{
    var matching = $(arr).filter(function(index, elem)
    {
        return elem[property] === value;
    }); 
    return matching.length > 0;
};

this returns true if it found the element in the array (makes it more readable this way), so you should also alter the check to

if( $.findFromArray('id', id, items) )

removed the !

Sign up to request clarification or add additional context in comments.

3 Comments

But I always returns TRUE, FALSE should be returned when the item already exists in the array
@csotelo, nope. The method I posted returns true if the element exists in the array or false if it does not. If you want to invert the result change the last line to return matching.length === 0
Oops sorry! I put the function in an alert ... but change the if statement and now it works great! thank you!
1

You've mixed up the order of index and element in $.each, and you only return from inside the $.each function, not the $.findFromArray function :

$.findFromArray = function(property, value, arr)
    var ret=false;
    $.each(arr, function(index, elem) {
        if (elem[property] === value) ret=true;
    }); 
    return ret;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.