Elie Saad Elie Saad - 3 months ago 22
Javascript Question

JSLINQ groupBy error

I am trying to manipulate a json file, so I am trying JSLINQ, but I can't figure out why I hit an error at groupBy().
The website that led me to this code.

var query = JSLINQ(json);
var result = query.groupBy(function (i) { //HERE is where the error hits.
return i.CustomerName; //Attribute of json
})
.select(function (i) {
console.log(i);
return {
CustomerName: i.Key, data: i.elements //I read that I get groupBy result like this.
.select(function (j) {
x = j.x, y = j.y //x and y are attributes
})
}
}).toArray();



query.groupBy is not a function

War War
Answer

Ask and ye shall receive young padawan ...

var result = jslinq(data)
   .groupBy(function (i) { return i.CustomerName; })
   .select(function(g) {
      return {
         key: g.key,
         items: jslinq(g.elements).select(function(i) { return { x: i.x, y: i.y } }).items
      };
    })
    .toList();

console.log(result);

....

Key differences between yours and mine ...

  • jslinq has been lowered in the version listed on github
  • element collections in the groups need to be wrapped in jslinq() too to be queried
Comments