【问题标题】:How to do a two different array results manipulation in angular6如何在angular6中进行两个不同的数组结果操作
【发布时间】:2019-04-26 19:27:54
【问题描述】:

这是产品服务响应。

productServiceResponse = [
  {productOrderId:108898,productStatus:"C",productId:'4'},
  {productOrderId:108899,productStatus:"P",productId:'2'},
];

通过使用常量值 currentProductStatus = "C";

在产品服务响应中,我需要找到当前产品并使用上述常量值获取产品并获取产品ID。

所以在这里我做到了。在这里我做了当前产品的索引,而不是动态地从集合中找出需要它的索引。

this.productServiceResponse[0].productStatus

//查看当前产品

  const ProductCode =
            this.productServiceResponse[0].productStatus ===
            this.currentProductStatus
              ? this.productServiceResponse[0].productId.toString()
              : null;

一次获取当前productId。

我已经在另一个集合中检查了它。

      productTypes =[
    {id: "1", name: "Laptop"},
    {id: "2", name: "Mobile"},
    {id: "3", name: "headphone"},
    {id: "4", name: "Cameras"}
      ];

从产品类型集合中,我已匹配上述结果中的产品 ID。

   this.currentProduct = this.productTypes.find(product=>product.id ===ProductCode).name;

最终的 o/p 变成了相机。

我有 o/p :但我需要在单行过滤中使用此查询而不是两个变量。

任何人都可以重构这个。

演示:filter

【问题讨论】:

  • 结果是错误的 o/p 应该是 camera 而不是 both ,因为 productStatus 是 C for product Id {productOrderId:108898,productStatus:"C",productId:'4'}
  • 我为您提供了灵活的解决方案,可以正确映射所有内容。现在你只需拿起你需要的索引;)

标签: angular typescript rxjs angular6 rxjs6


【解决方案1】:

试试这个方法

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); 低于错误 _this.productServiceResponse.find(...).map is not a function
  • 当然,因为map 是在object 而不是array 上运行,因为find 返回(第一个)object 满足给定的条件。
  • 只需将 .map 从代码中取出,并将其保留为 .productId 和 .name 我就这样编辑了我的答案
  • @deezig o/p 在您的代码中是错误的 没有检查 currentProductStatus 结果是错误的 o/p 应该是相机而不是两者,因为 productStatus 是 C 的产品 ID {productOrderId:108898, productStatus:"C",productId:'4'}
  • @Gabriel Lopez 谢谢你的解决方案中的嵌套操作。在 stackoverflow 中保持支持
猜你喜欢
  • 2020-07-04
  • 1970-01-01
  • 2022-01-14
  • 1970-01-01
  • 2013-05-15
  • 1970-01-01
  • 2011-12-29
  • 2021-06-18
  • 2023-03-18
相关资源
最近更新 更多