DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Vladimir has posted 1 posts at DZone. View Full User Profile

Prototype Objects/array/collector Example

11.02.2009
| 3405 views |
  • submit to reddit
        
var MyObj={
   obj1:{
    prop: ['А', 'C', 'D', 'E']
   },
   obj2:{
    prop: ['B', 'C', 'D', 'E']
   },
   obj3:{
    prop: ['А',  'E']
   },
   obj4:{
    prop: ['D', 'E']
   }
}

var newObj = {}

Object.keys(MyObj).each(function(k,v){
   MyObj[k].prop.each(function(x,y){
     if(typeof newObj[x]=="undefined"){newObj[x]=[]}
     newObj[x].push(k)
   })
})

Result:

B: ["obj2"]
C: ["obj1", "obj2"]
D: ["obj1", "obj2", "obj4"]
E: ["obj1", "obj2", "obj3", "obj4"]
А: ["obj1", "obj3"]