function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Naitik RaiNaitik Rai 

Replace Key with prefix from a key in JavaScript

Hello Friends,

Anyone Please help!
//JavaScript Code
component.set("v.SelectedProcessRecords",response.getReturnValue());
console.log('v.SelectedProcessRecords ----> '+component.get("v.SelectedProcessRecords"));

I am getting this in console log.

v.SelectedProcessRecords ----> [{"Id":"a017F00000JjtWDQAZ","Name":"Test Process Step","Tmp__Team_Assigned__c":false,
"Tmp__Assigned_To_Role__c":"Accounting Manager","Tmp__Completed_Date__c":"2018-02-09","Tmp__Status__c":"Completed"}]

I want to remove 'Tmp__' from all keys if it is present somewhere, and i want a new array of object like this.
[{"Id":"a017F00000JjtWDQAZ","Name":"Test Process Step","Team_Assigned__c":false,
"Assigned_To_Role__c":"Accounting Manager","Completed_Date__c":"2018-02-09","Status__c":"Completed"}]

Any help would be greatly appreciated.
Best Answer chosen by Naitik Rai
VaasuVaasu
Naitik,

Please try this,it should work. First connvert json object to string and replace adn then convert back it  to json object and set it to component.
var jsonData = response.getReturnValue();
var str = JSON.stringify(jsonData).replace(/Tmp__/g, "");
console.log('After Replacing TMP= '+str);
//Convert back string to JSON object
var jsonObject = JSON.parse(str);
console.log('JSON Object = '+jsonObject);
component.set("v.SelectedProcessRecords",jsonObject);

Response:
After Replacing TMP= [{"Id":"a017F00000JjtWDQAZ","Name":"Test Process Step","Team_Assigned__c":false,"Assigned_To_Role__c":"Accounting Manager","Completed_Date__c":"2018-02-09","Status__c":"Completed"}]
 

All Answers

Raj VakatiRaj Vakati
Here is the code. Tmp__ is coming from the namespace. Please cross check 

 
var jsonData = [{"Id":"a017F00000JjtWDQAZ","Name":"Test Process Step","Tmp__Team_Assigned__c":false,
"Tmp__Assigned_To_Role__c":"Accounting Manager","Tmp__Completed_Date__c":"2018-02-09","Tmp__Status__c":"Completed"}];


for(var obj in jsonData){
    if(jsonData.hasOwnProperty(obj)){
    for(var prop in jsonData[obj]){
        if(jsonData[obj].hasOwnProperty(prop)){
			     prop = prop.replace("Tmp__", "");
				 jsonData[prop] = prop ; 
				 
		console.log(prop);
	 
        }
    }
}
}

console.log(jsonData);



component.set("v.SelectedProcessRecords",jsonData);

 
VaasuVaasu
Assuming you would not get "TMP__" in the values
var str = response.getReturnValue();
str = str.replace('Tmp__','');
component.set("v.SelectedProcessRecords",str);
Naitik RaiNaitik Rai

Hi Raj & Vaasu, thanks for the response guys,

@Raj V
at line no 12, i am getting all keys without namespace.
but at line 19, again i am getting the same(with namespace) key/value pair which you are assigning at line no 1.
and same thing you are setting at line no 23.

@Vaasu
I am getting error: $A.getCallback() [str.replace is not a function].
 

Raj VakatiRaj Vakati
Can you share the code .. you shouldn't get the namespace  

str.replace  will not work any how .. 
Naitik RaiNaitik Rai
Hi,
Please review this.
 
var jsonData = response.getReturnValue();
console.log('jsonData ----> '+jsonData);                                                                                                         
for(var obj in jsonData ){
	 if(jsonData.hasOwnProperty(obj)){
		for(var prop in jsonData[obj]){
			if(jsonData[obj].hasOwnProperty(prop)){
				prop = prop.replace("Tmp__", "");
				 jsonData[prop] = prop ;
				console.log('prop---->>>> '+prop);
			}
		}
	}
}
console.log('jsonData --->>>>> '+jsonData );
component.set("v.SelectedProcessRecords",jsonData);
console.log('v.SelectedProcessRecords----> '+component.get("v.SelectedProcessRecords"));

 
Raj VakatiRaj Vakati

You are printing wrong data on console log ...  That where you confused ..  Setting data is correct. 


Below link is worng 
console.log('v.SelectedProcessRecords----> '+component.get("v.SelectedProcessRecords"));


Modify it to 

console.log('v.SelectedProcessRecords----> '+jsonData );


 
Naitik RaiNaitik Rai
@Raj
I don't think, this line may cause error.
because, i am printing 
console.log('jsonData --->>>>> '+jsonData );
component.set("v.SelectedProcessRecords",jsonData);
console.log('v.SelectedProcessRecords----> '+component.get("v.SelectedProcessRecords"));
at line 2 here, i am setting jsonData to "v.SelectedProcessRecords" attribute,
and then i am printing the attribute "v.SelectedProcessRecords" which has got the data from jsonData already.

you are saying:
Below line is worng 
console.log('v.SelectedProcessRecords----> '+component.get("v.SelectedProcessRecords"));
Modify it to 
console.log('v.SelectedProcessRecords----> '+jsonData );

both line are same, they both will print same data on the console.
 
Raj VakatiRaj Vakati
can you share the complete code .. i am not seeing any issue so for .. 
Naitik RaiNaitik Rai
I have already provide the complete code for this context.
var jsonData = response.getReturnValue();
console.log('jsonData ----> '+jsonData);                                                                                                         
for(var obj in jsonData ){
	 if(jsonData.hasOwnProperty(obj)){
		for(var prop in jsonData[obj]){
			if(jsonData[obj].hasOwnProperty(prop)){
				prop = prop.replace("Tmp__", "");
				 jsonData[prop] = prop ;
				console.log('prop---->>>> '+prop);
			}
		}
	}
}
console.log('jsonData --->>>>> '+jsonData );
component.set("v.SelectedProcessRecords",jsonData);
console.log('v.SelectedProcessRecords----> '+component.get("v.SelectedProcessRecords"));
at line 2, 14, and 16 i am getting the same thing at console.
[{"Id":"a017F00000JjtWDQAZ","Name":"Test Process Step","Tmp__Team_Assigned__c":false,
"Tmp__Assigned_To_Role__c":"Accounting Manager","Tmp__Completed_Date__c":"2018-02-09","Tmp__Status__c":"Completed"}]

at line 12, i am getting keys without namesapce on console like this(thats fine).

prop---->>>> Id
prop---->>>> Name
prop---->>>> Account_Team_Assigned__c
prop---->>>> Assigned_To_Role__c
prop---->>>> Completed_Date__c
prop---->>>> Status__c

but i want to print final output on console without namespace on keys like this:
[{"Id":"a017F00000JjtWDQAZ","Name":"Test Process Step","Team_Assigned__c":false,
"Assigned_To_Role__c":"Accounting Manager","Completed_Date__c":"2018-02-09","Status__c":"Completed"}]
 
VaasuVaasu
Naitik,

Please try this,it should work. First connvert json object to string and replace adn then convert back it  to json object and set it to component.
var jsonData = response.getReturnValue();
var str = JSON.stringify(jsonData).replace(/Tmp__/g, "");
console.log('After Replacing TMP= '+str);
//Convert back string to JSON object
var jsonObject = JSON.parse(str);
console.log('JSON Object = '+jsonObject);
component.set("v.SelectedProcessRecords",jsonObject);

Response:
After Replacing TMP= [{"Id":"a017F00000JjtWDQAZ","Name":"Test Process Step","Team_Assigned__c":false,"Assigned_To_Role__c":"Accounting Manager","Completed_Date__c":"2018-02-09","Status__c":"Completed"}]
 
This was selected as the best answer
Naitik RaiNaitik Rai

Thanks Vaasu.

It works perfect now.