js产品列表不会得到.length

时间:2013-12-19 17:58:58

标签: javascript jquery arrays

所以使用JS创建'产品'

<script type="text/javascript" language="javascript">
                    var products = [
                    @{
            bool addComma = false;
            foreach (UMACS.Bradmount.Models.QBProductRecord product in Model.AllProducts)
            {
                if (addComma)
                {
                                <text> @Html.Raw(",") </text>
                }

                            <text> @Html.Raw(string.Format("{{ value: \"{0} ({1})\", label: \"{0} ({1})\", desc: \"{1}\", prodid: \"{2}\" }}", product.ProductCode.Stripped(), product.ProductName.Stripped(), product.Id)) </text>

                addComma = true;
            }
                    }
                    ];

所以这个产品类似:

//                        {
//                            value: "CARSLI5",
//                            label: "CARSLI5",
//                            desc: "Carrots Sliced 5kg",
//                            prodid: "43"
//                        },
//                        {
//                            value: "CARBAT5",
//                            label: "CARBAT5",
//                            desc: "Carrots Baton 5kg",
//                            prodid: "24"
//                        }
//                    ];

然后我想说

self.selectedProductId = ko.computed(function() {
                        for (var i = 0; i < products.length; i++) {
                            if (products[i].label == self.selectedProduct()) {
                                return products[i].prodid;
                            }
                        }       
                        return "0";

其中products.length表示产品不存在?关于为什么的任何想法?

1 个答案:

答案 0 :(得分:1)

JS列表看起来应该是var list = [["a","b"],["c","d"],["e","f"]]; []而不是{}

list.length将提供3

所以list[0].length给出2