I have the following object:
{ apple: 'banana',
  banana: [ 'pear', 'apple' ],
  melon: 'apple',
  grapes: 'peach',
  carrot: 'apple',
  peach: 'grapes' }
I am basically tying to find any 'circular references', for example:
apple: 'banana',
banana: ['apple']
and
grapes: 'peach',
peach: 'grapes'
I've spent ages and have tried a number of different approaches, including copying the key and values into a new array, sorting and trying to match - now I'm not even sure what the best way to tackle this is.
Edit: Thanks to everyone for their help. I think it wasn't quite clear with my original question. I was only wanting to identify the case where there exists a reference in both directions. So apple->banana, banana<-apple and grapes->peach, peach<-grapes should match. But melon->apple, banana->apple and carrot->apple should not match at all.
I've got it working with the following (fairly disgraceful) code:
var data = { apple: 'banana',
  banana: ['test', 'apple'],
  melon: 'apple',
  grapes: 'peach',
  carrot: 'apple',
  peach: 'grapes' };
var arr = [], arr2 = [], u = {} 
//Iterate over object 'data' and create an array of arrays - sorted alphabetically
for (var item in data) {
    var val = data[item];
      if (Array.isArray(val)) {
        for (var i = 0; i < val.length; ++i) {
          arr.push(new Array(item, val[i]).sort())
        }
    } else {
      arr.push(new Array(item, val).sort())
    }
}
//Iterate of arr and look for any matches..
for(var i = 0, l = arr.length; i < l; ++i){
  if(u.hasOwnProperty(arr[i])) {
         arr2.push(arr[i]);
      }
      u[arr[i]] = 1;
}
console.log('Matches found: ' + arr2)
//prints: 'Matches found: [ [ 'apple', 'banana' ], [ 'grapes', 'peach' ] ]
 
     
     
    