Way to extract values and then generate options tag

I have the following data I’m receiving from the service. The following console logs prints the following in browser:

console.log("Inside data");

Inside data 
[[id=1, name=BOTTLE-1], [id=2, name=BOTTLE-2], [id=4, name=BOTTLE-3]]

If I want to build a options tag using jQuery, is it easy to extract the id as 1,3 and 4 and name for the value parameter of options tag and build a select dropdown? I’m using jQuery.

For example if I’ve this JSFiddle and if I want to work it like it is showing now. Building part using append method should be easy but I am wondering if it’s easy to extract the required values

1 Like

Are you really sure, that the data looks like that? Because that is no valid data at all. I cannot imagine, that a service is delivering such a useless format.

1 Like

Specifically, Jack, Thallius is pointing out that what you’ve told us the service is giving you isnt a recognized format. If it’s meant to be JSON, look very carefully - the difference between [] and {} is vital.

1 Like

Yeah, it’s not a JSON data. So I have this going on.

But I’m wondering if it’s possible to not hard code BOTTLE from the line let codingRegex = /id=\d+, name=BOTTLE-\d+/g; as that name can be different or if I can include a variable name in place of BOTTLE. I tried like this using RegExp but that seems to be failing.

Does this do what you want?

Yes, thanks! Do you think jQuery can be used here?

I am not very familar with jQuery but I don’t think it would help much here. You could use it insted of my use of document.getElementsByName().

Not many people seem to be using jQuery around here.

BTW: I should have pointed out to you that, as it stands, my regular expression allows the value of ‘name=’ to have alphanumeric word characters and and can include one or more “-” characters.

It does also assume perfect and exact formatting. So make sure you’ve got it exactly as the service is going to render it every time.
If there’s a space anywhere in that data, it collapses the matches; if the data is missing any attributes, it collapses the matches. (So if name would ever be multiple words, does the service wrap it in quotes? Is there ever a case where it WOULD be multiple words? We dont know.)

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.