gpt4 book ai didi

javascript - 我如何从另一个对象获取数据?

转载 作者:行者123 更新时间:2023-11-30 09:24:10 24 4
gpt4 key购买 nike

Plunker我有两个结构 - 配料和食谱

[{
"id":"1",
"name": "Cucumber"
},
..
]

 [{
"id":"1",
"name": "Salad1",
"recipein":[1, 3, 5]
}, {
...
}
]

我想通过按一个按钮来显示每份沙拉中的配料名称。我过滤了对象以获得对象的 ID,然后我尝试获取一组成分

getSalad(param:number) {
this.saladId = this.recipe.filter(rec => {
return rec.id.includes(param);
})

this.getNameOfIngredients(this.saladId)
}

getNameOfIngredients(saladArray:any) {
var ingredientsId = saladArray.map(function(num) {
return num.recipein;
});

我得到数组 [1,2,4] 现在我想用这个 id 数组显示 this.ingredients 中的所有成分名称。 我怎样才能做到这一点?

Plunker

最佳答案

我在你的 plunker 中进行了更新。我认为这就是您要找的东西:Plunker

  getSalad(param:number) {

this.saladId = this.recipe.filter(rec => +rec.id === param )[0];
if(!this.saladId){
this.currentSalad = "Salad not found";
return;
}
this.currentSalad = this.getNameOfIngredients(this.saladId)

}

getNameOfIngredients(saladArray:any) {
return this.ingredients.filter( ing => {
return saladArray.recipein.indexOf(+ing.id) !== -1;
});

关于javascript - 我如何从另一个对象获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49870912/

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