在javascript中过滤对象数组

时间:2014-04-23 14:54:19

标签: javascript arrays object

我是JS的新手,现在我已经完成了一项任务,希望有人可以指导我完成任务。

我有一个对象数组,如下所示:

var labels = [
            // labels for pag 1
            {pageID:1, labels: [
                {labelID:0, content:[{lang:'eng', text:'Txt1 Eng'}, {lang:'de', text:'Txt1 De:'}]},
                {labelID:1, content:[{lang:'eng', text:'Txt 2 Eng:'}, {lang:'de', text:'Txt2 De:'}]},
                {labelID:2, content:[{lang:'eng', text:'Txt 3 Eng:'},{lang:'de', text:'Txt 3 De:'}]}
            ]},

            // labels for pag 2
            {pageID:2, labels: [
                {labelID:0, content:[{lang:'eng', text:'Txt1 Eng'}, {lang:'de', text:'Txt1 De:'}]},
                {labelID:1, content:[{lang:'eng', text:'Txt 2 Eng:'}, {lang:'de', text:'Txt2 De:'}]},
                {labelID:2, content:[{lang:'eng', text:'Txt 3 Eng:'},{lang:'de', text:'Txt 3 De:'}]}
            ]}
    ]

我要做的是编写一个函数来返回特定页面和特定语言的标签(对象)数组。通过调用此函数指定pageID 1和lang eng,我基本上尝试构建一个像这样的数组:

var desideredArray = [
    {labelID:0, text:'Txt1 Eng'}, 
    {labelID:1, text:'Txt1 Eng'}, 
    {labelID:2, text:'Txt2 Eng'}
] 

现在,我正在尝试编写函数来检索/构建新数组:

this.getLabelsForPageAndLang = function (numPage, lang) {
            // this part filters the main object and selects the object with pageID == numPage
        var result = labels.filter(function( obj ) {
            return obj.pageID == numPage;
        });

        var tempResult = result[0].labels;
        var desiredResults = [];  // here I want to store the new objects
        for (var i=0; i<tempResult.length; i++) {
            var simpleLabelObject = {};
            simpleLabelObject.labelID = tempResult[i].labelID;
            // simpleLabelObject.text = ?????

            results[i] = simpleLabelObject;
        }

        console.log (results);

    };

...但是如何在内容属性中访问正确的值(与所选lang相对应的值)?

3 个答案:

答案 0 :(得分:1)

您可以使用与保持匹配页面的技术相同的技术:filter方法。

this.getLabelsForPageAndLang = function (numPage, lang) {
        // this part filters the main object and selects the object with pageID == numPage
    var result = labels.filter(function( obj ) {
        return obj.pageID == numPage;
    });
    var contentFilter = function(obj){ return obj.lang === lang};

    var tempResult = result[0].labels;
    var desiredResults = [];  // here I want to store the new objects
    for (var i=0; i<tempResult.length; i++) {
        var simpleLabelObject = {};
        simpleLabelObject.labelID = tempResult[i].labelID;
        var matching = tempResult[i].content.filter(contentFilter);
        simpleLabelObject.text = matching[0].text;

        desiredResults[i] = simpleLabelObject;
    }

    console.log (desiredResults);

};

我没有进行绑定检查,因为在你的代码中你假设总有一个匹配的元素,但这样做可能是明智的。

如果你想在每次调用函数时避免创建两个闭包,你可以为此创建一个对象的原型:

var Filter = function(numPage, lang) {
    this.numPage = numPage;
    this.lang = lang;
};

Filter.prototype.filterPage = function(obj) {
    return obj.pageID === this.numPage;
}

Filter.prototype.filterLang = function(obj) {
    return obj.lang === this.lang;
}

Filter.prototype.filterLabels = function(labels) {
    var result = labels.filter(this.filterPage, this);

    var tempResult = result[0].labels;
    var desiredResults = [];  // here I want to store the new objects
    for (var i=0; i<tempResult.length; i++) {
        var simpleLabelObject = {};
        simpleLabelObject.labelID = tempResult[i].labelID;
        var matching = tempResult[i].content.filter(this.filterLang, this);
        simpleLabelObject.text = matching[0].text;

        desiredResults[i] = simpleLabelObject;
    }

    return desiredResults;
}

console.log(new Filter(1, "eng").filterLabels(labels));

答案 1 :(得分:1)

再次过滤:

var getLabelsForPageAndLang = function (numPage, lang) {
    // this part filters the main object and selects the object with pageID == numPage
    var result = labels.filter(function (obj) {
        return obj.pageID == numPage;
    }); 
    var tempResult = result[0].labels;
    var desiredResults = []; // here I want to store the new objects
    for (var i = 0; i < tempResult.length; i++) {
        var simpleLabelObject = {};
        simpleLabelObject.labelID = tempResult[i].labelID;
        var lg = tempResult[i].content.filter(function (lg) {
            return lg.lang == lang;
        });
        simpleLabelObject.text = lg[0].text;

        desiredResults.push(simpleLabelObject);
    }

    console.log(desiredResults);

};

http://jsfiddle.net/9q5zF/

答案 2 :(得分:0)

一个相当安全的&#39;当页面具有相同的pageID且具有相同lang的多个内容时的实例:

this.getLabelsForPageAndLang = function(numPage, lang) {
    var result = [];
    var pages = labels.filter(function( obj ) {
        return obj.pageID === numPage;
    });
    for (var p = pages.length - 1; p >= 0; p--) {
        var page = pages[p];
        for(var i = page.labels.length - 1; i >= 0; i--) {
            var labelId = page.labels[i].labelID;
            for (var j = page.labels[i].content.length - 1; j >= 0; j--){
                if (page.labels[i].content[j].lang === lang) {
                    result.push({labelID: labelId, test: page.labels[i].content[j].text});
                }
            }
        }
    }    
    console.log(result);
}

小提琴:http://jsfiddle.net/6VQUm/