【问题标题】:How to group the elements in an array based on status property in react?如何根据反应中的状态属性对数组中的元素进行分组?
【发布时间】:2020-12-22 00:24:08
【问题描述】:

我有一个对象数组,即在我的反应状态下的 JSON,在渲染它之前,我希望它根据状态属性示例进行排序- 实际价值

{
"list": [
{
   "id": "aac",
   "name": "James",
   "status": "active"
},
{
   "id": "gds",
   "name": "Alice",
   "status": "left"
},
{
   "id": "cvs",
   "name": "Kristy",
   "status": "active"
},
{
   "id": "cgt",
   "name": "Samaira",
   "status": "left"
},
]
}

期望值根据状态元素排序后

{
"list": [
{
   "id": "aac",
   "name": "James",
   "status": "active"
},
{
   "id": "cvs",
   "name": "Kristy",
   "status": "active"
},

{
   "id": "gds",
   "name": "Alice",
   "status": "left"
},

{
   "id": "cgt",
   "name": "Samaira",
   "status": "left"
},
]
}

【问题讨论】:

标签: javascript arrays reactjs react-native lodash


【解决方案1】:

这将按状态按字母顺序排序

arrayName.sort((objA, objB) => objA.status > objB.status ? 1 : -1)

【讨论】:

  • 谢谢!有效。不知道我也可以对字符串使用排序方法。但我学到了:-p
猜你喜欢
  • 2019-09-10
  • 1970-01-01
  • 2021-09-02
  • 1970-01-01
  • 1970-01-01
  • 2017-03-18
  • 1970-01-01
  • 1970-01-01
  • 2019-02-02
相关资源
最近更新 更多