简体   繁体   English

Javascript函数返回false甚至找到对象

[英]Javascript function returning false even object found

I have a Product javascript array, which contains all the products information. 我有一个Product javascript数组,其中包含所有产品信息。 Created a function which iterate on this array and find the product by matching id. 创建了一个迭代此数组并通过匹配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? 函数每次都返回false,即使找到匹配的产品id,也不知道为什么? 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. 但这不是正确的方法,因为我想停止迭代并在找到对象时返回值。

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;
}

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. 你总是从findProduct返回false,如果找到你从$.each()回调方法返回的项,但是这没有反映在findProduct方法返回的值中。

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;
}

No need for jQuery's each function. 不需要jQuery的每个功能。 Use libraries only when they are needed. 仅在需要时使用库。

The problem is, that the 'each' method, is in fact ANOTHER function. 问题是,'each'方法实际上是另一种功能。 Returning from it means nothing to the 'findProduct' function. 从它返回对'findProduct'函数没有任何意义。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM