mihaifm / linq

linq.js - LINQ for JavaScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

can't easily convert to javascript Map

jmagaram opened this issue · comments

Maybe I'm missing something here, but when you have an enumerable you can to toDictionary but you can't do toMap to create a regular javascript map. Very confused. I think ENikS/LINQ allows you to do that quite simply.

They seem to be having the same source code in ENikS/LINQ
https://github.com/ENikS/LINQ/blob/master/lib/linq.ts#L463

It's a bit tricky.
What is the scenario where you need this ? You could use get or set, or even dictionary.buckets (not recommended).

Suppose you have an array of numbers and want to group them by even or odd and then put them in a JavaScript Map<boolean, number[]>. I can't figure out how to do this. Want someghing like this...

const groups = Linq
    .from([1, 2, 3, 4, 5, 6])
    .groupBy(i => (i % 2 === 0), i => i)
    .map(i=>[i.key, i]) // can't call filter or map or anything here
    .toJavascriptMap();

You can get an array:

var Linq = require('linq')
var groups = Linq
    .from([1, 2, 3, 4, 5, 6])
    .groupBy(i => (i % 2 === 0), i => i)
    .first()
    .toArray()
//  .map(i=>[i.key, i]) 
console.log(groups);

See https://repl.it/@mspclaims/LimpYoungAssembler