Reordering the JSON data by fields in Angular 6

94 views Asked by At

I am using JSON data, there I want to reorder it according to the need. For example: If my json data is like this,

this.allFruits = [
  { CD: 'APP', DSC: 'Apple', HID: 'HID1' },
  { CD: 'LEM', DSC: 'Lemon', HID: 'HID2' },
  { CD: 'LIM', DSC: 'Lime', HID: 'HID3' },
  { CD: 'ORA', DSC: 'Orange', HID: 'HID4' },
  { CD: 'BAN', DSC: 'Banana', HID: 'HID5' }];

I want it to be like this,

this.allFruits = [
  { HID: 'HID1', CD: 'APP', DSC: 'Apple'},
  { HID: 'HID2', CD: 'LEM', DSC: 'Lemon'},
  { HID: 'HID3', CD: 'LIM', DSC: 'Lime'},
  { HID: 'HID4', CD: 'ORA', DSC: 'Orange'},
  { HID: 'HID5', CD: 'BAN', DSC: 'Banana'}];

I am using Angular 6 typescript and have tried something like this:

var k = JSON.parse(JSON.stringify( this.allFruits, ["HID","CD","DSC"] ,4));

But it is not giving the desired format. Even if I give the format like this ["DSC","CD"], it should show only these two. Please help me out in this.

0

There are 0 answers