Home > Mobile >  How to pull specific values from a string?
How to pull specific values from a string?

Time:08-09

The project I am working on is a todo-list that has the ability to render a pdf using jsPDF. I need to be able to extract or pull (I am not sure what the correct codding term would be) specific attributes from that string so that I can print it as text using jsPDF.

The string I have is:

[{"content":"Test","category":"important","done":false,"createdAt":1659908150914},{"content":"Clean Room","category":"important","done":false,"createdAt":1659912937851}]

Now there are a few factors that make this complicated. Firstly, I only want the value(s) in quotes after content. In this case that would be Test and Clean Room. The other difficult part would be assigning them each a separate ID so that they can be printed using jsPDF.

I have spent a while trying to figure out the best way to do it, but this is actually my first coding project, so I am not quite sure what to do.

Any ideas?

CodePudding user response:

First of all, if it's a string, you have to convert it into a json object with the JSON.parse() method, like so

var jsobj = JSON.parse('[{"content":"Test","category":"important","done":false,"createdAt":1659908150914},{"content":"Clean Room","category":"important","done":false,"createdAt":1659912937851}]');

Then you simply access to your content field like this:

jsobj.forEach(k => {
          
             console.log(k.content);

          });
  • Related