-1

I'm using associative array to create json objects, serialize and send them to a third party:

var MSG = {}
MSG["SESSION_START"] = 0x0000;
MSG["DONE"]          = 0x0001;

var session_id = gen_rand_no();
var msg_tuple = {MSG["SESSION_START"] : session_id};
var json_msg_tuple = JSON.stringify(msg_tuple);

send(json_msg_tuple); 

The party would normally reply with an acknowledgement message in a form of a serialized JSON object:

var serialized_json = recv();
var json_obj = JSON.parse(serialized_json); 

How can I check if the first element in the JSON object is corresponding to the MSG["DONE"] value?

In the answer of this question, it is assumed that the JSON object has a given set of attributes until the nested ones are iterated over.

Community
  • 1
  • 1
Sebi
  • 4,262
  • 13
  • 60
  • 116
  • Probable dupe: http://stackoverflow.com/questions/909003/javascript-getting-the-first-index-of-an-object – Marc B Jul 07 '16 at 14:34
  • 3
    I don't think `{MSG["SESSION_START"] : session_id}` is valid JavaScript. – gen_Eric Jul 07 '16 at 14:36
  • First of all, you're just using JavaScript objects. JSON is a format for strings. Because of that I think you're overthinking this. Just set a breakpoint and inspect what `json_obj` has on it. You can probably just use `json_obj['DONE']`... – Heretic Monkey Jul 07 '16 at 14:36
  • @Rocket Hazmat I only need a unique mapping between a descriptive string message and a 2 byte integer value. – Sebi Jul 07 '16 at 14:41
  • @Sebi: Yeah, but your `msg_tuple` is a syntax error. – gen_Eric Jul 07 '16 at 14:46
  • So, what *exactly* is in `json_obj`? What does `console.log(json_obj)` show you? – gen_Eric Jul 07 '16 at 14:47
  • It's a json obj. of a serialized tuple of the form . What is wrong with the syntax of the tuple assignment? – Sebi Jul 07 '16 at 14:49
  • It shows a syntax error at the tuple assignment. – Sebi Jul 07 '16 at 14:56
  • 1
    JavaScript doesn't have "tuples", per se. Also you can use `MSG.SESSION_START` instead of `MSG["SESSION_START"]`. – gen_Eric Jul 07 '16 at 15:07
  • 1
    In order to use a *variable* value as a key, you need to do: `var msg_tuple = {}; msg_tuple[MSG["SESSION_START"]] = session_id;`. In ES6, however, you can do: `var msg_tuple = {[MSG["SESSION_START"]]: session_id};` – gen_Eric Jul 07 '16 at 15:08

2 Answers2

2

Objects in JavaScript have no "key order" so there is no such thing as a "first element".

With that in mind, the following two options exist for testing if the key exists:

if( MSG['DONE'] in json_obj)
// or...
if( json_obj.hasOwnProperty(MSG['DONE']))
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Although, if you for some reason don't know the name of the key you could do this: `Object.keys(json_obj)[0]` – Scott Finlay Jul 07 '16 at 14:39
  • What would be the behavior if the second item in the tuple would also have the value 0x0000 and the first one a distinct value? – Sebi Jul 07 '16 at 15:25
2

In modern browsers (IE9+, FF4+, Chrome5+, Opera12+, Safari5+) you could use the built in Object.keys method:

var keys = Object.keys(json_obj);
if (json_obj[keys[0]] === MSG["DONE"]) { ... }

Does this answer your question? Is it the value of the first json_obj attribute that you want to compare to the value of MSG["DONE"]? Or did I misread your question?

P.S. I tried the Object.keys() method and it returns the keys in the order they are listed in the object, but I am not sure if this is guaranteed.

Jean-François Beauchamp
  • 5,485
  • 8
  • 43
  • 77