English 中文(简体)
Javascript Arrayindex First
原标题:Javascript Array indexFirst

我建立了处理网页的原型,我成功地增加了(粉碎),但能够获取数据,我失败:

var foundImageIndex = Pages.indexFirst(function (item) { if (item.PageID == PageID) return true; });

The javascript pagehandler:

var Pages = new Array();

PageContainer = function () //constructor for the proxy
{
   // this._baseURL = url;
};

PageContainer.prototype =
{

    AddPage: function (data) {
        if (data == null) return;
        Pages.push({ PageID: data.PageID, SegmentID: data.SegmentID });
    },

    GetPage: function (PageID) {
        alert( getPage( +PageID+ )=  + JSON.stringify(Pages));

        var foundImageIndex = Pages.indexFirst(function (item) { if (item.PageID == PageID) return true; });

        var dt = { PageID: Pages[foundImageIndex].PageID, SegmentID: Pages[foundImageIndex].SegmentID };
        return dt;
    }


};

我呼吁其他各位:

var gPageContainer = new PageContainer();

 for (var i = 0; i < SegStruct.SegmentsCount; i++) {
                var segRClass = //get from webservice
                gPageContainer.AddPage({ PageID: i, SegmentID: segRClass.SegmentID });

            }

I trying to call: gPageContainer.GetPage(1); but it failed in GetPage: function (PageID) it returns -1 in: var foundImageIndex = Pages.indexFirst(function (item) { if (item.PageID == PageID) return true; });

发现 图Index一贯-1

为什么?

问题回答

在施工人员面前添加以下内容:

if (typeof Array.prototype.indexFirst ==  undefined ) {
    Array.prototype.indexFirst = function (validator) {
        for (var i = 0; i <= this.length - 1; i++) {
            if (validator(this[i])) {
                return i;
            }
        }
        return -1;
    };
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签