0

I have a string containing a list of dictionary. I want to compare by checking their keys not value. is there any way to do that.

data = '[{ "group1" : "T1"}, { "group1" : "T2"}, { "group2" : "T3"}]';

how can i compare these dictionaries on the basis of their keys. for example, i have used:

dataObj = eval(data);  //generate 3 objects for data.

here dataObj[0] will refer to object of { "group1" : "T1"} to do it i have done following code:

for(i=0;i<dataObj.length;i++){
if (dataObj[i].key == dataObj[i].key){         //<-- i am not getting what to do to compare their keys i.e. "group1" and "group1"
//my other statements
}
}
star
  • 151
  • 1
  • 2
  • 11
  • That's not how objects work. Please have a look at the MDN JavaScript guide: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects. Related: http://stackoverflow.com/q/11922383/218196. – Felix Kling Apr 10 '13 at 10:01
  • 2
    So are you trying to see whether one object's key(s) is the same as another object's key(s)? – Qantas 94 Heavy Apr 10 '13 at 10:01
  • yes @Qantas 94 Heavy. Is there any way to do so. I am new to the javascript. need a good clarification regarding that. – star Apr 10 '13 at 10:05
  • 1
    @star: So which are you trying to compare, and what do you want to occur? – Qantas 94 Heavy Apr 10 '13 at 10:18
  • Additionally, there seems to be no reason to `eval` a string rather than just writing that code initially: `var dataObj = [{"group1" : "T1"}, { "group1" : "T2"}, { "group2" : "T3"}];`. Eval is generally considered to be evil, as it makes code harder to follow amongst other things, and should be avoided unless absolutely necessary. – Andrzej Doyle Apr 10 '13 at 10:22
  • I want here to compare first objects key i.e group1 with second objects key which is also group1. if both are same then T1 and T2 (keys values) will be consider as sibling. If both are not same for example if next object key has group2 then group1 will be parent and group2 willbe its child. – star Apr 10 '13 at 10:24

1 Answers1

1

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" 
ZER0
  • 24,846
  • 5
  • 51
  • 54
  • Thanks for the suggestion. Here i want to check whether one object's key(s) is the same as another object's key(s) or not. so that i can use its value i.e. T1 and T2 according to it. – star Apr 10 '13 at 10:32