簡體   English   中英

用Javascript組嵌套數組

[英]Group nested array in Javascript

我有一些用於以下可視化的數據數組,格式如下

 var Dataset1 = [ { "commentBy" : "saurabh", "comment" : "Testing", "datestamp" : "07/07/2017", "weekcount" : 1 }, { "commentBy" : "raman", "comment" : "Planning", "datestamp" : "07/07/2017", "weekcount" : 1 }, { "commentBy" : "Execution", "comment" : "Alfa Beta", "datestamp" : "07/07/2017", "weekcount" : 2 }, { "commentBy" : "Execution", "comment" : "Zseta Gama", "datestamp" : "07/07/2017", "weekcount" : 2 } ] //although i have tried writing this function but this is not giving me the desired result. var groupBy = function(xs, key) { return xs.reduce(function(rv, x) { (rv[x[key]] = rv[x[key]] || []).push(x); return rv; }, {}); }; var groubedByTeam=groupBy(Dataset1, 'weekcount') console.log(groubedByTeam); 

我想按周計數對數據集進行分組,以便所需的結果應像這樣。

    [
    { "weekcount" : 1
       "grouped" : [
          {   "commentBy" : "saurabh",
              "comment" : "Testing",
              "datestamp" : "07/07/2017"
          }, 
          {
               "commentBy" : "raman",
               "comment" : "Planning",
              "datestamp" : "07/07/2017"
           }
      ]    
    }, {
      "weekcount" : 2
          "grouped" : [
           {
        "commentBy" : "Execution",
        "comment" : "Alfa Beta",
        "datestamp" : "07/07/2017",

      },
        {
        "commentBy" : "Execution",
        "comment" : "Zseta Gama",
        "datestamp" : "07/07/2017",

      } 
      ]    
    }
 ]

這是對數據進行分組的一種簡單方法,您應該能夠以此為出發點,弄清楚如何格式化數據。

grouped = {}

Dataset1.forEach(function(item, index){

    if (!grouped[item.weekcount]) grouped[item.weekcount] = [];
    grouped[item.weekcount].push(item);

});

grouped的對象是具有周計數的鍵。 如果對象中不存在某個特定的周數作為鍵,則創建一個空數組,然后將數據壓入該數組。 在以后的迭代中,具有相同周數的數據將添加到現有數組中。

您可以檢查從0到最大值的每個Weekcount,然后過濾數組。 可能是這樣的:

 var Dataset1 = [ { "commentBy" : "saurabh", "comment" : "Testing", "datestamp" : "07/07/2017", "weekcount" : 1 }, { "commentBy" : "raman", "comment" : "Planning", "datestamp" : "07/07/2017", "weekcount" : 1 }, { "commentBy" : "Execution", "comment" : "Alfa Beta", "datestamp" : "07/07/2017", "weekcount" : 2 }, { "commentBy" : "Execution", "comment" : "Zseta Gama", "datestamp" : "07/07/2017", "weekcount" : 2 } ] var maxWeekCount = 3; var result = [] for(var i=0; i<maxWeekCount; i++){ var group = Dataset1.filter(obj => obj.weekcount === i) if(group.length) { result.push({ weekCount: i, grouped: group }) } } console.log(result) 

使用一個維護對weekcount對象的引用的輔助對象,將數組簡化為分組結構。

 var Dataset1 = [{"commentBy":"saurabh","comment":"Testing","datestamp":"07/07/2017","weekcount":1},{"commentBy":"raman","comment":"Planning","datestamp":"07/07/2017","weekcount":1},{"commentBy":"Execution","comment":"Alfa Beta","datestamp":"07/07/2017","weekcount":2},{"commentBy":"Execution","comment":"Zseta Gama","datestamp":"07/07/2017","weekcount":2}]; var helperMap = {}; var result = Dataset1.reduce(function(arr, obj) { var current = helperMap[obj.weekcount]; if(!current) { current = { weekcount: obj.weekcount, grouped: [] }; helperMap[obj.weekcount] = current; arr.push(current); } current.grouped.push({ commentBy: obj.commentBy, comment: obj.comment, datestamp: obj.datestamp }); return arr; }, []); console.log(result); 

var groupBy = function(xs, key) {
  return xs.reduce(function(rv, x) {
    if(rv[x[key]] == undefined){
      rv[x[key]] = {"weekcount": x[key], "grouped": []}
    }
    stripped = {}
    for(var k in x) if(k!=key) stripped[k]=x[k]; //strip "key" property
    rv[x[key]]["grouped"].push(stripped);
    return rv;
  }, []);
};

通過剝離“鍵”屬性,此解決方案可在不修改任何輸入的情況下使用,因此,如果您從輸入中添加/刪除某些屬性,則它仍將按預期工作,以反映更改。

const formatted = [];

Dataset1.forEach((data) => {
  const { weekcount, comment, commentBy, datestamp } = data;
  let obj = formatted.find((item) => item.weekcount === weekcount);

  if (!obj) {
    formatted.push({
      weekcount,
      grouped: [{
        comment,
        commentBy,
        datestamp
      }]
    })
  } else {
    obj.grouped.push({
      comment,
      commentBy,
      datestamp
    });
  }
});

 const Dataset1 = [{ "commentBy": "saurabh", "comment": "Testing", "datestamp": "07/07/2017", "weekcount": 1 }, { "commentBy": "raman", "comment": "Planning", "datestamp": "07/07/2017", "weekcount": 1 }, { "commentBy": "Execution", "comment": "Alfa Beta", "datestamp": "07/07/2017", "weekcount": 2 }, { "commentBy": "Execution", "comment": "Zseta Gama", "datestamp": "07/07/2017", "weekcount": 2 }]; const formatted = []; Dataset1.forEach((data) => { const { weekcount, comment, commentBy, datestamp } = data; let obj = formatted.find((item) => item.weekcount === weekcount); if (!obj) { formatted.push({ weekcount, grouped: [{ comment, commentBy, datestamp }] }) } else { obj.grouped.push({ comment, commentBy, datestamp }); } }); console.log(formatted); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM