You need to sign in to do that
Don't have an account?
Naitik Rai
Replace Key with prefix from a key in JavaScript
Hello Friends,
Anyone Please help!
//JavaScript Code
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.
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.
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
var str = response.getReturnValue();
str = str.replace('Tmp__','');
component.set("v.SelectedProcessRecords",str);
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].
str.replace will not work any how ..
Please review this.
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 );
I don't think, this line may cause error.
because, i am printing 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.
[{"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"}]
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"}]
Thanks Vaasu.
It works perfect now.