简体   繁体   English

从两层深层对象中提取价值

[英]Pulling Out Value from Within Two-Levels Deep Object

I am trying to retrieve one particular value from within a two-levels deep object data structure. 我正在尝试从两级深度对象数据结构中检索一个特定的值。 First off, though, I am saving into a variable within the function, like this: 不过,首先,我要保存到函数中的变量中,如下所示:

getTargetId() {
    if (this.authenticationService.isAuthenticated()) {
        const userInfo = sessionStorage.getItem('currentUser');
        console.log(userInfo);
    }
}

From: 从:

console.log(userInfo);

I get this back in the console: 我在控制台中得到这个:

{"token":"sometoken.value","data":{"_id":"8cd0362c0", "phone":"555-4343"...}

What I want to do is specifically pull out the "_id" value here. 我想要做的是在这里专门拉出“ _id”值。

I tried: 我试过了:

console.log(userInfo.data._id); console.log(userInfo.data._id);

But then my IDE is showing me an error: 但是随后我的IDE向我显示了一个错误:

'Property '_id' does not exist on type 'string'. 类型'string'上不存在'属性'_id'。

How do I dig out "_id" in this case? 在这种情况下,如何挖掘“ _id”?

You are accessing it wrong 您访问错误
Try userInfo.data._id 试试userInfo.data._id
In the log of your object you can see by the {} notation that data is another object, so after accessing data you can access its properties just as you would with any other object. 在对象的日志中,您可以使用{}表示法表示data是另一个对象,因此在访问data您可以像访问任何其他对象一样访问其属性。

I also see that you are getting 我也看到你越来越

'Property '_id' does not exist on type 'string'. 类型'string'上不存在'属性'_id'。

This could mean that you never parsed the information. 这可能意味着您从未解析过该信息。 To find out if this is the case this should be right: 要找出是否是这种情况,应该正确:

Running-> 运行->

console.log(userInfo);

Returns-> 返回->

{"token":"sometoken.value","data":{"_id":"8cd0362c0", "phone":"555-4343"...}

Just after this code: 在此代码之后:
Running-> 运行->

console.log(typeof userInfo);

Returns-> 返回->

"string"

With your edits, I can see that this is the case. 通过您的编辑,我可以看到情况确实如此。
Try: 尝试:

userInfo = JSON.parse(sessionStorage.getItem('currentUser') );
console.log(userInfo.data._id);

The _id property is under the data key: _id属性在data键下:

 const response = { "token":"sometoken.value", "data": { "_id":"8cd0362c0", "phone":"555-4343" } }; console.log(response.data._id) 

You can also use destructuring: 您还可以使用解构:

const { _id } = response.data;
console.log(_id)

or: 要么:

const { data: { _id }} = response;
console.log(_id);

So, as @jonsharpe pointed out, the key was to JSON.parse the string first. 因此,正如@jonsharpe所指出的那样,关键是JSON.parse首先是字符串。 So this gets me the value I need for "_id": 因此,这为我提供了我需要的“ _id”值:

getTargetId() {
    if (this.authenticationService.isAuthenticated()) {
        const userInfo = JSON.parse(sessionStorage.getItem('currentUser'));
        console.log(userInfo.data._id);
    }
}

Actually your string is returned as JSON string. 实际上,您的字符串是作为JSON字符串返回的。 So you have to parse it into object using JSON.parse() if you are using js or with $.parseJSON() if you are using Jquery. 因此,如果使用js,则必须使用JSON.parse()将其解析为对象;如果使用Jquery,则必须使用$ .parseJSON()进行解析。 So your updated code now looks like this. 因此,您的更新代码现在看起来像这样。

 var user ='{"token":"sometoken.value","data":{"_id":"8cd0362c0", "phone":"555-4343"}}'; var k = JSON.parse(user); alert(k.data._id); 

And Fiddle is here . 小提琴在这里 Thank You 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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