How to parse an array of a JSON object with conflict key case sensitive | angular | typescript | javascript

Let’s see the example array of JSON like:

const category = [

      { ID: ‘1’, name: ‘Helper’ },

      { Id: ‘2’, name: ‘Script’ },

    ];

Above array of json has key ID of in the first index and then in next index key Id. Both keys are different case sensitive. So let see the simple solution for the above.

Solution:

ngOnInit(){

const category = [

      { ID: ‘1’, name: ‘Helper’ },

      { Id: ‘2’, name: ‘Script’ },

    ];

    const modifycategory = [];

    category.forEach((val, index) => {

      const newobj = {Id: ”, Name: ”};

      const keys = Object.keys(val);

      newobj.Id = val[keys[0]];

      newobj.Name = val[keys[1]];

      modifycategory.push(newobj);

     });

    console.log(modifycategory);

}

Console- Output:

 [      { ID: ‘1’, name: ‘Helper’ },      { Id: ‘2’, name: ‘Script’ }    ]

Hope this will helpful to you. If any queries about this. let me know in the below comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *