gpt4 book ai didi

javascript - 在javascript中过滤对象数组

转载 作者:行者123 更新时间:2023-11-30 17:30:15 26 4
gpt4 key购买 nike

我真的是 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);

};

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

最佳答案

您可以使用与保留匹配页面相同的技术: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);

};

我没有进行绑定(bind)检查,因为在您的代码中您假定始终存在匹配元素,但这样做可能是明智的。

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

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

关于javascript - 在javascript中过滤对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23248007/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com