-1

I want to rename the factORLossTree into savefactORLossTree dynamically inside the function from below payload.

I am getting below data on payload after submitting the form.

{
    "cluster":"Europe",
    "factory":"Caivano",
    "factoryId":"Caivano",    
    "factORLossTree":[
       {
          "skuid":"000000000067334539",
          "skuDescription":"MAG 55ml Mini PistHazelnut 8MP x6x120 EB",         
          "levelLosses":[
             {
                "level1":"Line Scheduling Losses",
                "variancePer":100
             }
          ],
          "isRowChecked":false
       }
    ],
    "submitType":"po"
 }

Below is my code .

saveOrUpdateORData() { 
    const formData = Object.assign({}, this.orLosstreeForm.value);
    if (formData.factORLossTree.length === 0) {
      this.dialogService.openDialog('Data Not Available');
      return false;
    }  
    console.log(formData,"formdata");
    return;   
  }

Expected Output

{
    "cluster":"Europe",
    "factory":"Caivano",
    "factoryId":"Caivano",    
    "savefactORLossTree":[
       {
          "skuid":"000000000067334539",
          "skuDescription":"MAG 55ml Mini PistHazelnut 8MP x6x120 EB",         
          "levelLosses":[
             {
                "level1":"Line Scheduling Losses",
                "variancePer":100
             }
          ],
          "isRowChecked":false
       }
    ],
    "submitType":"po"
 }

Can anyone please help me to do this.

Pravesh Singh
  • 23
  • 1
  • 6
  • _"...rename the factORLossTree into savefactORLossTree dynamically..."_ - What does this mean? What is the expected result? And what have you tried so far to solve this on your own? – Andreas Aug 09 '22 at 12:39
  • @Andreas, I have added my expected output. i want if i post form values so before posting into API i need to rename the key factORLossTree into savefactORLossTree – Pravesh Singh Aug 09 '22 at 12:58
  • Duplicate of: [JavaScript: Object Rename Key](https://stackoverflow.com/questions/4647817/javascript-object-rename-key) or [Rename the object key](https://stackoverflow.com/questions/62544266/rename-the-object-key) or https://duckduckgo.com/?q=javascript+object+rename+(property+OR+key)+site%3Astackoverflow.com – Andreas Aug 09 '22 at 13:55

1 Answers1

0

Sure thing! Worth mentioning that this issue is purely JS and has nothing to do with your framework (e.g. - Angular)

To the point: let's assume that your payload is stored in a variable called payload

First - check if the property exists. Then - replace it:

const propertyName = 'put the property name here'
const newPropertyName = 'put the new property name here'

if (payload.hasOwnProperty(propertyName)) {
  payload[newPropertyName] = payload[propertyName]
  delete payload[propertyName]
}

Why this is working? Because we are creating another reference to the original data before deleting the property. At the end we end-up with one reference having the updated name

If (from some reason) you need to clone the data, follow this pattern instead:

if (payload.hasOwnProperty(propertyName)) {
  const clone = JSON.parse(JSON.stringify(payload[propertyName]))
  payload[newPropertyName] = clone
  delete payload[propertyName]
}

I assume that your property always contains an object. If the type of the property is primitive (e.g. - number, string or boolean) you can just skip the cloning phase all together (primitives are copied by their values while only objects are handled by their reference)

ymz
  • 6,602
  • 1
  • 20
  • 39
  • I tried below code bt it is showing error on propertyname Cannot find factorlosstree if (formData.hasOwnProperty(factORLossTree)) { formData[savefactORLossTree] = formData[factORLossTree] delete formData[factORLossTree] } – Pravesh Singh Aug 09 '22 at 12:54
  • 1
    I suggest that you will use a sandbox to apply the code and see the results. Once you get how it works - you may make the additional adjustments to your code in order to resolve the issue. also you should use `'factORLossTree'` instead of just `factORLossTree`.. just saying – ymz Aug 09 '22 at 13:29
  • Why did you add an answer instead of a close-vote as an [obivious duplicate](https://duckduckgo.com/?q=javascript+object+rename+(property+OR+key)+site%3Astackoverflow.com) of on of the many "rename a property" questions here on SO? – Andreas Aug 09 '22 at 13:52
  • Hi @Andreas. Do to the complexity and low up-voting of the answers for this topic (at least those I found) - I thought a straight forward answer may help. If you feel otherwise, you can mark it as duplicate.. NP – ymz Aug 09 '22 at 14:21
  • After OP edited the question I definitely would close it as duplicate, but I've "wasted" my CV for the previous unclear version of the question... – Andreas Aug 09 '22 at 14:30
  • 1
    I think OP should always get a chance to redeem the question .. (in question) like they did here. Out of curiosity what does CV stand for in this context @Andreas ? – Tommi Aug 09 '22 at 14:47
  • CV = close vote – Andreas Aug 09 '22 at 15:11