Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Do not use <code>eval</code> but JSON.parse](<a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/parse" rel="nofollow">https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/parse</a>) instead. If it's not available in some old browsers there are plenty of implementation that are less vulnerable to attack than just <code>eval</code>.</p> <p>Saying that, having:</p> <pre><code>var data = '[{ "group1" : "T1"}, { "group1" : "T2"}, { "group2" : "T3"}]'; var array = JSON.parse(data); </code></pre> <p>Will generate an Array of Object, each of them has one own property called <code>group1</code> or <code>group2</code>. To be clear:</p> <pre><code>var myobj = {"group1" : "T1"} </code></pre> <p>Is an <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Values,_variables,_and_literals?redirectlocale=en-US&amp;redirectslug=Core_JavaScript_1.5_Guide%2FValues%2C_Variables%2C_and_Literals#Object_literals" rel="nofollow">Object literal</a>, and it's exactly the same of:</p> <pre><code>var myobj = new Object(); myobj.group1 = "T1"; </code></pre> <p>So, as you can see, there is no <code>key</code> property anywhere.</p> <p>It's not clear what you want to compare, also because the third object seems have a different property's name.</p> <p><strong>Updated</strong>: 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:</p> <pre><code>array[0].group1 === array[1].group1 // false, one is "T1", one is "T2" </code></pre> <p>If you want to have a <code>key</code> properties that contains the value <code>"group1"</code>, than your objects needs to be different:</p> <pre><code>var data = '[{"key" : "group1", "value": "T1"}, {"key": "group1", "value" : "T2"}, { "key" : "group2", "value" : "T3"}]' </code></pre> <p>Then, you can have:</p> <pre><code>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" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload