简体   繁体   中英

How to pull specific values from a string?

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?

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);

          });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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