我有一个字符串形式的本地存储密钥。外观如下:
git
在控制台上打印:
spring:
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo
仅供参考:var key = localStorage.getItem(localStorage.key(0));
console.log(key);
我要删除此部分 {"logged_user":"myname","abcDatatitle":"AHarmbca Reports","abcData":"300"};
,以便密钥console.log(typeof key); prints string
如下所示:
"abcDatatitle":"AHarmbca Reports"
我可以像这样使用javascript的(console.log(key);)
函数
{"logged_user":"myname","abcData":"300"};
,但这只会删除此replace
部分。考虑到以下情况,如何确定整个key = key.replace("abcDatatitle","");
部分都消失了
这个值abcDatatitle
是动态值。
答案 0 :(得分:1)
您可以使用Delete
例如
var myObject = {"logged_user":"myname","abcDatatitle":"AHarmbca Reports","abcData":"300"};
delete myObject.abcDatatitle
console.log(JSON.stringify(myObject));
答案 1 :(得分:1)
使用JSON.parse
,然后删除对象密钥,然后使用JSON.stringify
//convert string to object
var key = localStorage.getItem(localStorage.key(0))
var objKey = JSON.parse(key)
delete objKey.abcDatatitle
//parse to string again
var strKey = objKey = JSON.stringify(objKey)
// update localStorage
localStorage.setItem(localStorage.key(0), strKey)
答案 2 :(得分:1)
你可以做到
//the key is localStorage.key(0)
localStorage.setItem(localStorage.key(0),'{"logged_user":"myname","abcDatatitle":"AHarmbca Reports","abcData":"300"}')
// we get the object data using JSON.parse(string)
obj = JSON.parse(localStorage.getItem(localStorage.key(0)));
// we delete the property
delete obj.abcDatatitle
// we store the new object as string using JSON.stringify(json)
localStorage.setItem(localStorage.key(0),JSON.stringify(obj));