I am new to JavaScript & haven't done much work with it, but have mainly experience with UNIX. I have a piece of code where I want to grep (excuse the UNIX language :D) for a id and get the number from that.
& then want to take the number which would be the event id? Not sure how possible this is within JavaScript but something along those lines like you would do with a UNIX shell script.
Thank you for your response!. I understand what you are saying & makes sense. Thanks very much. My only issue is that the value "event ID" is dynamic & runs through an API so I don't run nor set the values in an object if that makes sense?
I see no problem in testing dynamic values coming from whatever source. As long as you can grab the data all is fine.
And since you have example data, the data is obviously there... . If it helps you describe further more in detail what you are doing. Maybe one can give additional hints.
Since you have a grip of what you want to do it's the only task to work out the code for your thoughts.
is a JSON property sent by the API as a key : value pair. Of course the value of this property will change, but the key is the same.
Remember, JSON is an object with key : value pairs.
Before you start parsing JSON objects, you should probably take a step-back and try to understand what a JSON object is and how they are used in APIs. Because, if you understood the bare minimum about JSON and Javascript you would not ask "how do I grep a Javascript JSON object to get a value based on a key"... which is essentially what you asked in your original post.
There is a big reason why JSON APIs are the most popular APIs on the web today; and there is a reason that most web developers process JSON objects with Javascript. It is trivial to access values in the object (as long as the JSON object is formatted correctly, LOL) and you don't need to write parsers, use 'grep', or 'awk' or 'sed' or anything like that when you are using Javascript.
You just receive the JSON object via your API and deference the key as I illustrated to get the value associated with the key. Of course the value changes, but the key does not change. If the key changed, 'grep' would not help you either, because you must know what the keys are in your API, that is the very, most "API 101" basic, fundamental thing you need to know with working with API and JSON with key:value pairs.
WIkipedia ... background info for you:
.
Note that the wikipedia blurb above calls the key-value pairs, attribute-values ; but it is the same thing, just a different name.
And forget the "XML" part of the blurb above. JSON is much simpler and easier to use for APIs. When I just coded the new "computer science trivia" feature here, all the APIs return JSON objects. These days, every API I write all return JSON; and when I build some new mobile forum pages later this year, all will be based on JSON API calls.
OBTW, I don't have the exact JSON API output in front of me and your JS code which calls the API, so this might be way off base; but this code seems overly complex to me; but then again, I don't have the JS code which calls the API or example JSON data from the API.
If you want me to take a closer look; please post your JS code which calls the API and some sample JSON output from the API.