2

I have an object like

{Text Field 1: {fieldName: "Text Field 1", fieldType: "text", data: "custom value"},
Text Field 2: {fieldName: "Text Field 3", fieldType: "text", data: "new value "},
Text Field 3: {fieldName: "test kuyans 2", fieldType: "dropdown", value: Array(2), data: "value"}

I want to change it keys

Text Field 1 , Text Field 2, Text Field 3 to string 1, string 2, string 3.

Final result will be

{string 1: {fieldName: "Text Field 1", fieldType: "text", data: "custom value"},
string 2: {fieldName: "Text Field 3", fieldType: "text", data: "new value "},
string 3: {fieldName: "test kuyans 2", fieldType: "dropdown", value: Array(2), data: "value"}

Could someone please help me how to resolve this issue .

Thanks

code

Object.fromEntries(
    Object.entries(JSON.parse(item.optional_fields)).map(([key, value]) =>
      // Modify key here
      [`${headerProjectFloor[index]}`, value]
    )
  )
devserkan
  • 16,870
  • 4
  • 31
  • 47
React Guy
  • 35
  • 1
  • 9

2 Answers2

0
obj['string 1'] = obj['Text Field 1']; // Assign new key 
delete obj['Text Field 1']; // Delete old key
Tushar Kale
  • 159
  • 1
  • 12
0

Using array reduce method you can do that,

var res = Object.keys(obj).reduce((prev, curr, index) => { return {...prev, ['string '+(index+1)]: obj[curr]}}, {});
console.log(res);
Md Sabbir Alam
  • 4,937
  • 3
  • 15
  • 30