簡體   English   中英

jQuery grep在對象數組上不起作用,但是for循環可以

[英]jQuery grep not working on object array but for loop does

我在這里關注本教程: http : //ddmvc4.codeplex.com/了解基因敲除。 這是我第一次使用javascript,但我想我知道到目前為止我在做什么。

我有一個簡單的對象數組,如下所示:

var DummyCompetition = [
    {
        "Id": 1,
        "Sport": 'Powerlifting',
        "Title": 'Íslandsmeistaramót í klassískum kraftlyftingum',
        "Country": 'Iceland',
        "DateStart": new Date(2014, 2, 8),
        "DateEnd": new Date(2014, 2, 8)
    },
    {
        "Id": 2,
        "Sport": 'Powerlifting',
        "Title": 'Íslandsmeistaramót í kraftlyftingum',
        "Country": 'Iceland',
        "DateStart": new Date(2014, 4, 8),
        "DateEnd": new Date(2014, 4, 8)
    }
]

我嘗試在這樣的函數中過濾id

var currentCompetition = $.grep(DummyCompetition, function (c) {
        return c.Id == id;
    });

currentCompetition = new Competition(currentCompetition[0]);

其中id是從URL獲得的var id = url.substring(url.lastIndexOf('/') + 1);

如果運行頁面,則無法加載javascript,但是如果使用for循環過濾數組,則一切正常。

for (var i = 0; i < DummyCompetition.length; i++)
{
    if (DummyCompetition[i].Id == id)
    {
        var currentCompetition = new Competition(DummyCompetition[i]);
        break
    }
}

我究竟做錯了什么?

我發現了問題所在。 Visual Studio將_Layout.cshtml視圖的@Scripts.Render("~/bundles/jquery")_Layout.cshtml @RenderBody()行的下面,因此不會為我正在使用jQuery的視圖加載它。

將其移至@Scripts.Render("~/bundles/modernizr")進行修復的頂部。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM