Do not use eval
but JSON.parse](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/parse) instead. If it's not available in some old browsers there are plenty of implementation that are less vulnerable to attack than just eval
.
Saying that, having:
var data = '[{ "group1" : "T1"}, { "group1" : "T2"}, { "group2" : "T3"}]';
var array = JSON.parse(data);
Will generate an Array of Object, each of them has one own property called group1
or group2
. To be clear:
var myobj = {"group1" : "T1"}
Is an Object literal, and it's exactly the same of:
var myobj = new Object();
myobj.group1 = "T1";
So, as you can see, there is no key
property anywhere.
It's not clear what you want to compare, also because the third object seems have a different property's name.
Updated:
About your comment, the point here is that there is no "object key" but just object's properties. It means, you need to know the name of the "key" in advance in order to use it:
array[0].group1 === array[1].group1 // false, one is "T1", one is "T2"
If you want to have a key
properties that contains the value "group1"
, than your objects needs to be different:
var data = '[{"key" : "group1", "value": "T1"}, {"key": "group1", "value" : "T2"}, { "key" : "group2", "value" : "T3"}]'
Then, you can have:
var array = JSON.parse(data);
array[0].key === array[1].key // true, both are "group1"
array[0].value === array[1].value // false, one is "T1", one is "T2"