-1

I have array format like this

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"
  }
}]

and the required format is

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
  }
}]

here I need the value of the districts at the beginning of the array, other then districts all key and values will be dynamic, hence districts should be on the top of the array.

sreejithsdev
  • 1,202
  • 12
  • 26
vinuta
  • 423
  • 9
  • 29
  • Why do you wants this? – Prashant Pimpale Jan 17 '19 at 07:01
  • need to display data on the data-table and the first column should be districts – vinuta Jan 17 '19 at 07:03
  • 2
    Order of the properties does not matter in JSON. See this answer for more details: https://stackoverflow.com/a/16870531/4469840 – Adhyatmik Jan 17 '19 at 07:05
  • in table you can bind value order you want using {{}} interpolation – TheParam Jan 17 '19 at 07:07
  • but i need the district at the top because I have to display complain reports based on district, and complain are bal vivha, type etc.. and the array format which has district on the bottom shows on data table at the end – vinuta Jan 17 '19 at 07:10
  • i cannot bind the value using interpolation because my data is dynamic , {{column}} – vinuta Jan 17 '19 at 07:11
  • {{data.data[column]| json}} – vinuta Jan 17 '19 at 07:15
  • Your response is **invalid**! You **cannot** have multiple properties with the same `key`. There are 3 properties named `data` in your JSON. I think you should redefine the way you produce the response. – Harun Yilmaz Jan 17 '19 at 07:40
  • the data is valid, i just want the district key at the top – vinuta Jan 17 '19 at 08:03
  • @vinuta How can it be **valid** if there are multiple values with same key? Which data do you expect to get with `newresponse[0]['data']`? – Harun Yilmaz Jan 17 '19 at 08:25
  • Beside the point mentioned by @Adhyatmik I will also like to metion that `Bal Vivah: 1` this key name is invalid and it may not work – brk Jan 17 '19 at 08:26

1 Answers1

0

The object that you have posted is not a valid object. The data property is duplicate. If the object was valid, and you are trying to display the details as a key-value pair, you can use keyvalue pipe (https://angular.io/api/common/KeyValuePipe#keyvaluepipe). You can pass a comparator function which you can code to give you the districts as the first value.

Check this: https://stackblitz.com/edit/angular-keyvalue-comparator

Sachin Gupta
  • 4,981
  • 3
  • 16
  • 32