2016-08-13 64 views
0

我想操縱一個JSON文件,所以我嘗試JSLINQ,但我無法弄清楚爲什麼我在groupBy()中出現錯誤。 The website that led me to this code.JSLINQ groupBy錯誤

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不是一個函數

回答

2

問就必領受年輕的學徒......

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); 

....

關鍵你和我的差別...

  • jslinq已在github
  • 在組元素集合需要列出的版本被降低到被包裹在jslinq()太要被查詢