【问题标题】:How to sort array in typescript by value [duplicate]如何按值对打字稿中的数组进行排序[重复]
【发布时间】:2021-03-02 21:23:38
【问题描述】:

如何按 isActive 值“True”对这个对象数组排序?

[{src: "SafeUrlImpl", id: "10047", filename: "Verify Event screenshot .jpg", fileType: "1", isActive: false},
 {src: "SafeUrlImpl", id: "1006", filename: "Verify Event screenshot .jpg", fileType: "1", isActive: true},
 {src: "SafeUrlImpl", id: "10045", filename: "Verify Event screenshot .jpg", fileType: "1", isActive: false},
 {src: "SafeUrlImpl", id: "10045", filename: "Verify Event screenshot .jpg", fileType: "1", isActive: false}]

我需要一个基于 isActive 值 True 的输出,如下所示

 [{src: "SafeUrlImpl", id: "1006", filename: "Verify Event screenshot .jpg", fileType: "1", isActive: true},
 {src: "SafeUrlImpl", id: "10047", filename: "Verify Event screenshot .jpg", fileType: "1", isActive: false},
 {src: "SafeUrlImpl", id: "10045", filename: "Verify Event screenshot .jpg", fileType: "1", isActive: false},
 {src: "SafeUrlImpl", id: "10045", filename: "Verify Event screenshot .jpg", fileType: "1", isActive: false}]

【问题讨论】:

标签: arrays typescript sorting


【解决方案1】:

您可以使用.sort() 方法。

const result = data.sort((a,b) => +b.isActive - +a.isActive);

【讨论】:

  • 如果您看到以“这能回答您的问题吗?”开头的问题的评论后面跟着一个链接,点击链接,看看链接问题的答案是否回答了问题。如果是这样,请不要回答这个问题,因为它会分散答案和问题,使人们更难找到。
猜你喜欢
  • 2022-11-25
  • 1970-01-01
  • 2017-02-12
  • 2021-09-12
  • 2020-10-01
  • 1970-01-01
  • 2013-07-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多