0

I have an array of objects where I want to rename the key name as shown:

let Options = [{Code: 1, Label: 'abc'}, {Code: 2, Label: 'xyz'}, {Code: 3, Label: 'fgh'}];

I want to change the key 'Code' to 'value' as shown:

let Options = [{value: 1, Label: 'abc'}, {value: 2, Label: 'xyz'}, {value: 3, Label: 'fgh'}];

How I can accomplish the desired output.

Mohsin
  • 73
  • 2
  • 9
  • Please do some research before asking. It is trivial to find similar questions. – str Jun 23 '20 at 21:52

1 Answers1

2

You can do it using map like this:

let Options = [{Code: 1, Label: 'abc'}, {Code: 2, Label: 'xyz'}, {Code: 3, Label: 'fgh'}];
const res = Options.map(elem => {
  elem.value = elem.Code;
  delete elem.Code;
  return elem;
});

mgm793
  • 1,968
  • 15
  • 23