toDictionary()

The *toDictionary" linq operator returns an object that has, as properties, all of the values projected by the "key selector" function, and as values for those properties, the values projected by the "element selector" function. If the values projected by the key selector would results in duplicate properties, an exception will be thrown.

If no element selector is given, then the elements, themselves, are used as property values.
var css = [{prop: 'width', value: '20px'},
    {prop: 'height', value: '30px'},
    {prop: 'align', value: 'center'}];
    
var users = [{user: 'kjones', fullname: 'Kurtis Jones'},
    {user: 'psmith', fullname: 'Paul Smith'},
    {user: 'jallen', fullname: 'Jane Allen'}];
    
// obj = {width: '20px', height: '30px', align: 'center'}
var obj = $linq(css).toDictionary("x => x.prop", "x => x.value");

// user_lookup = {'kjones' : {user: 'kjones', fullname: 'Kurtis Jones'}, 
//                'psmith' : {user: 'psmith', fullname: 'Paul Smith'},
//                'jallen' : {user: 'jallen', fullname: 'Jane Allen'}};
var user_lookup = $linq(users).toDictionary("x => x.user");

Last edited Sep 11, 2012 at 12:12 AM by battousai999, version 1