0

I have a Product javascript array, which contains all the products information. Created a function which iterate on this array and find the product by matching id.

var products = JSON.parse('[{"Product":{"id":"1","name":"My Product","description":"This is my new product","price":"10.00","currency":"$","stock":"0","image":"/image1.png"}},{"Product":{"id":"5","name":"Dummy Product 2","description":"Some dummy text goes here.","price":"10.00","currency":"$","stock":"100","image":"image2.jpg"}}]');

$(document).ready(function(){
    console.log(products);
    alert(findProduct(5)); //it will returns false everytime, evan it has matching product
});

function findProduct(product_id){
    $.each(products, function(k, v){
        if(v.Product.id == product_id){
            console.log(v);
            return products[k]; //or return 'v'
        }
    });
    return false;
}

Check this Demo

Function returns false each time, even though it found the matching product id, don't know why? If I store the matching array key in a variable and after iteration, return the value of that key it returns proper object. But that's not proper way, cause I want to stop iteration and return the value if object found.

3 Answers 3

1

You are always returning false from findProduct, if the item is found you are returning from the $.each() callback method, but that is not reflected in the value returned by findProduct method.

function findProduct(product_id) {
    var found = false;
    $.each(products, function (k, v) {
        if (v.Product.id == product_id) {
            console.log(v);
            found = products[k];
            //return false to prevent further iteration
            return false;
        }
    });
    return found;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Use this instead:

function findProduct(product_id){
    var result = false;
    $.each(products, function(k, v){
        if(v.Product.id == product_id){
            console.log(v);
            result = products[k]; //or return 'v'
            return;
        }
    });
    return result;
}

And here is the problem:

function findProduct(product_id){
    $.each(products, function(k, v){
        if(v.Product.id == product_id){
            console.log(v);
            return products[k]; //returns from the $.each callback
        }
    });
    return false;
}

2 Comments

return result; maybe?
Yep, fixed it. Thanks for the note.
0

No need for jQuery's each function. Use libraries only when they are needed.

The problem is, that the 'each' method, is in fact ANOTHER function. Returning from it means nothing to the 'findProduct' function.

function findProduct(product_id){
    for(var k in products){
        var product = products[k];
        if(product && product.Product && product.Product.id == product_id)
            return product;
    }
    return false;
}

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.