Javascript 从本地存储器获取对象

Javascript 从本地存储器获取对象,javascript,angular,typescript,local-storage,angular8,Javascript,Angular,Typescript,Local Storage,Angular8,我有一个本地存储器,它存储一些数据。我试图使用localStorage.getItem('id')获取组件中的数据但它未定义。问题是,localStorage正在以奇怪的格式存储id。它像abcabc.id一样存储,现在localStorage中有多个这样的参数,对于每个登录的用户,字符串abcabc都会更改。我怎样才能仍然获得id值。我可以比较字符串或其他东西来获得值吗?比如如果它包含字符串.id只读取该值?如果是,你能解释一下这是如何实现的吗 本地存储: abcabcabc.username

我有一个本地存储器,它存储一些数据。我试图使用
localStorage.getItem('id')获取组件中的数据但它未定义。问题是,
localStorage
正在以奇怪的格式存储
id
。它像
abcabc.id
一样存储,现在localStorage中有多个这样的参数,对于每个登录的用户,字符串
abcabc
都会更改。我怎样才能仍然获得
id
值。我可以比较字符串或其他东西来获得值吗?比如如果它包含字符串
.id
只读取该值?如果是,你能解释一下这是如何实现的吗

本地存储:

abcabcabc.username.id = sdfsdfsdfs
abcabcabc.username.refreshToken = sdfsdfs
abcabcabc.username.accessToken = ertsdffdg

localStorage将数据设置为键值对的映射,因此:

var id = {key:"id", value:"sdfsdfsdfs"}
//JSON.stringify converts the JavaScript object id into a string 
localStorage.setItem('id', JSON.stringify(id));
现在,您可以通过以下方式获得数据:

var getTheId = localStorage.getItem('id');
//you can use JSON.parse() if you need to print it
console.log('getTheId: ', JSON.parse(getTheId));
var x = localStorage.getItem(id);//normally getting your id
//parse it
var st = JSON.parse(x);
var ids = [];
while(st.includes(id)){
//this is to only get the abcabacabc
var newId = st.substring(0,9);
ids.push(newId);
}
console.log(ids);
您通常会这样做,但由于我不知道您是如何设置数据的,因此您可以通过以下方式获取数据:

var getTheId = localStorage.getItem('id');
//you can use JSON.parse() if you need to print it
console.log('getTheId: ', JSON.parse(getTheId));
var x = localStorage.getItem(id);//normally getting your id
//parse it
var st = JSON.parse(x);
var ids = [];
while(st.includes(id)){
//this is to only get the abcabacabc
var newId = st.substring(0,9);
ids.push(newId);
}
console.log(ids);

因为您不知道localStorage中存储的确切密钥,所以请获取所有密钥,然后对其进行迭代。接下来,匹配您知道的部分密钥,即id,如下所示:

// fetch all key-value pairs from local storage
const localStorageKeys = { ...localStorage };

// iterate over them
for (const index in localStorageKeys) {

  // see if key contains id
  if (index.includes('id')) {

    // fetch value for corresponding key 
    console.log(localStorageKeys[index]);

  }
}

在没有其他参数的情况下保存您的id
localStorage.setItem('YourIdVaribale')
;问题是,我使用的是AWS Cognito,它只以特定的格式发送它。。。比如:
abcabc.username.id,abcabc.username.name
等等,并且没有办法只获取
id
。如何在typescript代码中实现这一点?我正在寻找类似于
if string.includes(id)return的内容
您无法在组件中获取用户名吗?(可能使用为当前用户存储数据的服务?)这样,您可以访问{username}。i您可以准确地执行
string.includes(id)
返回string.substring(0,9)
。0和9是为abcabct这是完美的@Nicholas K。谢谢你也可以看看这个:它是由我的一个朋友发布的。谢谢,不客气。不幸的是,这个问题太宽泛了。如果可以对它进行编辑,缩小到一个特定的问题,我很乐意尝试一下。干杯再次感谢。对于第二个问题,我在上面添加了一些代码和注释。我试图在
tokenExpiration
中获取到期时间,并与本地时间进行检查,以查看用户注销后的到期时间是否更长。
httpinterceptor
是否能够在条件生效或我需要手动触发器时自动检查它?你能解释一下它是如何工作的吗?Thank.intercept(请求:HttpRequest,下一步:HttpHandler):可观察的{this.token=JSON.parse(localStorage.getItem('tokenExpiration');if(this.tokenExpired(this.token)){Auth.signOut().then((res)=>{this.authState===='signedout';this.router.navigate(['/');});}else{return}private-tokenExpired(token:number){const-expire=token;return(Math.floor((新日期).getTime()/1000))>=expire;}