如何在angular6中处理两个不同的数组结果
这里是一个产品服务响应如何在angular6中处理两个不同的数组结果,angular,typescript,rxjs,angular6,rxjs6,Angular,Typescript,Rxjs,Angular6,Rxjs6,这里是一个产品服务响应 productServiceResponse = [ {productOrderId:108898,productStatus:"C",productId:'4'}, {productOrderId:108899,productStatus:"P",productId:'2'}, ]; 通过使用常量值currentProductStatus=“C” 在产品服务响应中,我需要找到当前产品,并使用上述常量值获取产品,并获取产品id 我在这里做了,我在这里做了当前产品
productServiceResponse = [
{productOrderId:108898,productStatus:"C",productId:'4'},
{productOrderId:108899,productStatus:"P",productId:'2'},
];
通过使用常量值currentProductStatus=“C”
在产品服务响应中,我需要找到当前产品,并使用上述常量值获取产品,并获取产品id
我在这里做了,我在这里做了当前产品的索引,而不是在DynamicLay中需要它,从集合中查找
this.productServiceResponse[0].productStatus
//检查当前产品
const ProductCode =
this.productServiceResponse[0].productStatus ===
this.currentProductStatus
? this.productServiceResponse[0].productId.toString()
: null;
一旦获得当前产品ID
我已经在另一个收藏中查过了
productTypes =[
{id: "1", name: "Laptop"},
{id: "2", name: "Mobile"},
{id: "3", name: "headphone"},
{id: "4", name: "Cameras"}
];
从ProductTypes集合中,我已经匹配了上面结果中的产品id
this.currentProduct = this.productTypes.find(product=>product.id ===ProductCode).name;
最终o/p变成摄像机
我已经得到了o/p:但是我需要在单行过滤中进行查询,而不是两个变量
任何人都可以重构它
演示:试试这种方法
this.productTypes.find(product=>
product.id === productServiceResponse.find(psr=>psr.productStatus === this.currentProductStatus).productID
).name
请注意,我在手机中键入了这个。我已将其更改为
this.productTypes.find(product=>{product.id==this.productServiceResponse.find(psr=>psr.productStatus==this.currentProductStatus).map(x=>x.productID)}.map(x=>x.name)代码>获取以下错误\u此.productServiceResponse.find(…).map不是一个函数确定,因为map
正在对象上运行,而不是array
因为find
返回(第一个)object
满足给定的条件。只需将.map从代码中取出,并将其保留为.productId和.name,我将答案编辑为so@deezig代码中的o/p错误没有检查currentProductStatus结果错误o/p应该是camera,而不是两者,因为productStatus是产品Id的C{productOrderId:108898,productStatus:'C',productId:'4'}@Gabriel Lopez谢谢你的解决方案中有嵌套操作。在StackOverflow中保持支持结果是错误的o/p应该是照相机,而不是两者都是,因为productOrderId:108898,productId:'4'}
我为您提供了灵活的解决方案,可以正确映射所有内容。现在您只需选择所需的索引;)