简体   繁体   中英

remove characters in quotes after a particular character jquery

I have a string like this

{'Salary':'Basic_Salary - ( ESI * .5 ) - Employee_PF + Bonus','Gross_Salary':'( Salary - Tax )','Employee_PF': ''}

If i have another string 'Salary':'',

I want to replace Salary and get the result

{'Salary':'Basic_Salary - ( ESI * .5 ) - Employee_PF + Bonus','Gross_Salary':'( Salary - Tax )','Employee_PF': ''}

I can easily check for 'Salary' . What I am stuck at is, how to replace string in '' after 'Salary':

You are basically working with JSON object which is coming as a string, in that case you can decode this string to JavaScript object and then modify the properties as any other object

var obj = jQuery.parseJSON({'Salary':'Basic_Salary - ( ESI * .5 ) - Employee_PF + Bonus','Gross_Salary':'( Salary - Tax )','Employee_PF': ''});

then :

obj.Salary = "Whatever you like";

then you can always convert it back to string with

JSON.stringify
var str = "{'Salary':'Basic_Salary - ( ESI * .5 ) - Employee_PF + Bonus','Gross_Salary':'( Salary - Tax )','Employee_PF': ''}";
var obj = JSON.parse(str);
    obj.Salary = '';
str = JSON.stringify(obj);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM