首页 > 解决方案 > 在数组的开头移动一个元素

问题描述

我有这样的数组格式

newresponse = [{
  data: {
    Bal Vivah: 1
    Type: 0
    districts: "East District"
  },
  data: {
    Bal Vivah: 1
    Type: 0
    districts: "West District"
  },
  data: {
    Bal Vivah: 1
    Type: 0
    districts: "North District"
  }
}]

并且所需的格式是

newresponse = [{
  data: {
    districts: "East District"
    Bal Vivah: 1
    Type: 0
  },
  data: {
    districts: "West District"
    Bal Vivah: 1
    Type: 0
  },
  data: {
    districts: "North District"
    Bal Vivah: 1
    Type: 0
  }
}]

这里我需要数组开头的区的值,除此之外,所有的键和值都是动态的,因此区应该在数组的顶部。

标签: javascriptangulartypescript

解决方案


您发布的对象不是有效对象。该data属性是重复的。如果对象有效,并且您尝试将详细信息显示为一key-value对,则可以使用keyvalue管道(https://angular.io/api/common/KeyValuePipe#keyvaluepipe)。您可以传递一个比较器函数,您可以对其进行编码以将区域作为第一个值。

检查这个:https ://stackblitz.com/edit/angular-keyvalue-comparator


推荐阅读