2

So I have a string[] variable and I want to only get the value of a specific key.

For example my string[] contains

{'firstname': 'john', 'lastname':'doe'}

In my view, I only want to display the last name. How do I do it?

{{myString.lastname}}  

gives me blank.

{{myString}}  

shows me

{'firstname': 'john', 'lastname':'doe'}
5
  • you said array? but you gave an Object. For Arrays, you need to specify the index as well. As for object, myString.lastname shiuld give you doe Commented Jan 14, 2018 at 20:27
  • I use string[] variable to store the json value I got from API. Not sure if this is the correct approach. Commented Jan 14, 2018 at 20:30
  • Showing a piece of code where you get the value from wouldn’t be a bad idea either Commented Jan 14, 2018 at 21:14
  • Storing an object in a string[] is likely why you have this problem. Try declaring it as myString: any = {}. Commented Jan 15, 2018 at 3:44
  • @JonathanLightbringer- Please store API result in an Object. Public result: object=[ ]; Commented Jan 15, 2018 at 4:57

1 Answer 1

6

You may need to parse the string to a JSON object if it is just a string

let myObj = JSON.parse(myString);

Then you can call

myObj.lastname
Sign up to request clarification or add additional context in comments.

4 Comments

If myString is actually a poorly named array, OP may need myString[0]
This might be what I'm looking for. However, I'm not sure where would I put it into my code since I assign my value through observable. Any ideas?
@JonathanLightbringer you can do .map((v) => JSON.parse(v)).sub.... before de subscribe
Hey @David i am having an nested JSON i want to access nested value any idea for that would help me .... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.