簡體   English   中英

按“扇區”關鍵字對嵌套對象數組進行分組 - Javascript

[英]Group an array of nested objects by 'sector' keyword - Javascript

我有一個 object,其中值是一個對象數組。 我想go到object按扇區分類。 這是我的數據。

{
   "Word 1":[
      {
         "name":"Walmart",
         "id":"Walmart",
         "no":1,
         "sector":"Retailing",
         "url":"https://www.walmart.com",
         "data":[
            {
               "year":2017,
               "totalCount":0
            },
            {
               "year":2018,
               "totalCount":2
            },
            {
               "year":2019,
               "totalCount":1
            },
            {
               "year":2020,
               "totalCount":1
            },
            {
               "year":2021,
               "totalCount":0
            }
         ]
      },
      {
         "name":"Amazon",
         "id":"amazon",
         "no":2,
         "sector":"Retailing",
         "url":"https://www.amazon.com/",
         "data":[
            {
               "year":2017,
               "totalCount":0
            },
            {
               "year":2018,
               "totalCount":0
            },
            {
               "year":2019,
               "totalCount":0
            },
            {
               "year":2020,
               "totalCount":5
            },
            {
               "year":2021,
               "totalCount":0
            }
         ]
      },
      {
         "name":"Apple",
         "id":"Apple",
         "no":3,
         "sector":"Technology",
         "url":"https://www.apple.com",
         "data":[
            {
               "year":2017,
               "totalCount":0
            },
            {
               "year":2018,
               "totalCount":0
            },
            {
               "year":2019,
               "totalCount":0
            },
            {
               "year":2020,
               "totalCount":0
            },
            {
               "year":2021,
               "totalCount":0
            }
         ]
      },
      {
         "name":"CVS Health",
         "id":"CVSHealth",
         "no":4,
         "sector":"Health Care",
         "url":"https://www.cvshealth.com",
         "data":[
            {
               "year":2017,
               "totalCount":0
            },
            {
               "year":2018,
               "totalCount":0
            },
            {
               "year":2019,
               "totalCount":1
            },
            {
               "year":2020,
               "totalCount":0
            },
            {
               "year":2021,
               "totalCount":0
            }
         ]
      }
   ],
   "Word 2":[
      {
         "name":"Walmart",
         "id":"Walmart",
         "no":1,
         "sector":"Retailing",
         "url":"https://www.walmart.com",
         "data":[
            {
               "year":2017,
               "totalCount":0
            },
            {
               "year":2018,
               "totalCount":0
            },
            {
               "year":2019,
               "totalCount":0
            },
            {
               "year":2020,
               "totalCount":1
            },
            {
               "year":2021,
               "totalCount":0
            }
         ]
      },
      {
         "name":"Amazon",
         "id":"amazon",
         "no":2,
         "sector":"Retailing",
         "url":"https://www.amazon.com/",
         "data":[
            {
               "year":2017,
               "totalCount":117
            },
            {
               "year":2018,
               "totalCount":0
            },
            {
               "year":2019,
               "totalCount":0
            },
            {
               "year":2020,
               "totalCount":0
            },
            {
               "year":2021,
               "totalCount":0
            }
         ]
      },
      {
         "name":"Apple",
         "id":"Apple",
         "no":3,
         "sector":"Technology",
         "url":"https://www.apple.com",
         "data":[
            {
               "year":2017,
               "totalCount":0
            },
            {
               "year":2018,
               "totalCount":3
            },
            {
               "year":2019,
               "totalCount":5
            },
            {
               "year":2020,
               "totalCount":3
            },
            {
               "year":2021,
               "totalCount":9
            }
         ]
      },
      {
         "name":"CVS Health",
         "id":"CVSHealth",
         "no":4,
         "sector":"Health Care",
         "url":"https://www.cvshealth.com",
         "data":[
            {
               "year":2017,
               "totalCount":0
            },
            {
               "year":2018,
               "totalCount":0
            },
            {
               "year":2019,
               "totalCount":0
            },
            {
               "year":2020,
               "totalCount":0
            },
            {
               "year":2021,
               "totalCount":0
            }
         ]
      }
   ],
   "Word 3":[
      {
         "name":"Walmart",
         "id":"Walmart",
         "no":1,
         "sector":"Retailing",
         "url":"https://www.walmart.com",
         "data":[
            {
               "year":2017,
               "totalCount":0
            },
            {
               "year":2018,
               "totalCount":0
            },
            {
               "year":2019,
               "totalCount":0
            },
            {
               "year":2020,
               "totalCount":0
            },
            {
               "year":2021,
               "totalCount":0
            }
         ]
      },
      {
         "name":"Amazon",
         "id":"amazon",
         "no":2,
         "sector":"Retailing",
         "url":"https://www.amazon.com/",
         "data":[
            {
               "year":2017,
               "totalCount":42
            },
            {
               "year":2018,
               "totalCount":0
            },
            {
               "year":2019,
               "totalCount":0
            },
            {
               "year":2020,
               "totalCount":0
            },
            {
               "year":2021,
               "totalCount":0
            }
         ]
      },
      {
         "name":"Apple",
         "id":"Apple",
         "no":3,
         "sector":"Technology",
         "url":"https://www.apple.com",
         "data":[
            {
               "year":2017,
               "totalCount":0
            },
            {
               "year":2018,
               "totalCount":3
            },
            {
               "year":2019,
               "totalCount":12
            },
            {
               "year":2020,
               "totalCount":3
            },
            {
               "year":2021,
               "totalCount":3
            }
         ]
      },
      {
         "name":"CVS Health",
         "id":"CVSHealth",
         "no":4,
         "sector":"Health Care",
         "url":"https://www.cvshealth.com",
         "data":[
            {
               "year":2017,
               "totalCount":13
            },
            {
               "year":2018,
               "totalCount":25
            },
            {
               "year":2019,
               "totalCount":26
            },
            {
               "year":2020,
               "totalCount":7
            },
            {
               "year":2021,
               "totalCount":3
            }
         ]
      }
   ]
}

這里的數據按關鍵字分類。 但是,我想按扇區對其進行分類,並獲取每個扇區的 totalCount 之和。

這就是我想要實現的。

{
   "Retailing":{
      "Word 1":9,
      "Word 2":118,
      "Word 3":42
   },
   "Technology":{
      "Word 1":0,
      "Word 2":20,
      "Word 3":21
   },
   "Health Care":{
      "Word 1":1,
      "Word 2":0,
      "Word 3":74
   }
}

這是我到目前為止所做的。

 const objectData = { "Word 1": [{ "name": "Walmart", "id": "Walmart", "no": 1, "sector": "Retailing", "url": "https://www.walmart.com", "data": [{ "year": 2017, "totalCount": 0 }, { "year": 2018, "totalCount": 2 }, { "year": 2019, "totalCount": 1 }, { "year": 2020, "totalCount": 1 }, { "year": 2021, "totalCount": 0 } ] }, { "name": "Amazon", "id": "amazon", "no": 2, "sector": "Retailing", "url": "https://www.amazon.com/", "data": [{ "year": 2017, "totalCount": 0 }, { "year": 2018, "totalCount": 0 }, { "year": 2019, "totalCount": 0 }, { "year": 2020, "totalCount": 5 }, { "year": 2021, "totalCount": 0 } ] }, { "name": "Apple", "id": "Apple", "no": 3, "sector": "Technology", "url": "https://www.apple.com", "data": [{ "year": 2017, "totalCount": 0 }, { "year": 2018, "totalCount": 0 }, { "year": 2019, "totalCount": 0 }, { "year": 2020, "totalCount": 0 }, { "year": 2021, "totalCount": 0 } ] }, { "name": "CVS Health", "id": "CVSHealth", "no": 4, "sector": "Health Care", "url": "https://www.cvshealth.com", "data": [{ "year": 2017, "totalCount": 0 }, { "year": 2018, "totalCount": 0 }, { "year": 2019, "totalCount": 1 }, { "year": 2020, "totalCount": 0 }, { "year": 2021, "totalCount": 0 } ] } ], "Word 2": [{ "name": "Walmart", "id": "Walmart", "no": 1, "sector": "Retailing", "url": "https://www.walmart.com", "data": [{ "year": 2017, "totalCount": 0 }, { "year": 2018, "totalCount": 0 }, { "year": 2019, "totalCount": 0 }, { "year": 2020, "totalCount": 1 }, { "year": 2021, "totalCount": 0 } ] }, { "name": "Amazon", "id": "amazon", "no": 2, "sector": "Retailing", "url": "https://www.amazon.com/", "data": [{ "year": 2017, "totalCount": 117 }, { "year": 2018, "totalCount": 0 }, { "year": 2019, "totalCount": 0 }, { "year": 2020, "totalCount": 0 }, { "year": 2021, "totalCount": 0 } ] }, { "name": "Apple", "id": "Apple", "no": 3, "sector": "Technology", "url": "https://www.apple.com", "data": [{ "year": 2017, "totalCount": 0 }, { "year": 2018, "totalCount": 3 }, { "year": 2019, "totalCount": 5 }, { "year": 2020, "totalCount": 3 }, { "year": 2021, "totalCount": 9 } ] }, { "name": "CVS Health", "id": "CVSHealth", "no": 4, "sector": "Health Care", "url": "https://www.cvshealth.com", "data": [{ "year": 2017, "totalCount": 0 }, { "year": 2018, "totalCount": 0 }, { "year": 2019, "totalCount": 0 }, { "year": 2020, "totalCount": 0 }, { "year": 2021, "totalCount": 0 } ] } ], "Word 3": [{ "name": "Walmart", "id": "Walmart", "no": 1, "sector": "Retailing", "url": "https://www.walmart.com", "data": [{ "year": 2017, "totalCount": 0 }, { "year": 2018, "totalCount": 0 }, { "year": 2019, "totalCount": 0 }, { "year": 2020, "totalCount": 0 }, { "year": 2021, "totalCount": 0 } ] }, { "name": "Amazon", "id": "amazon", "no": 2, "sector": "Retailing", "url": "https://www.amazon.com/", "data": [{ "year": 2017, "totalCount": 42 }, { "year": 2018, "totalCount": 0 }, { "year": 2019, "totalCount": 0 }, { "year": 2020, "totalCount": 0 }, { "year": 2021, "totalCount": 0 } ] }, { "name": "Apple", "id": "Apple", "no": 3, "sector": "Technology", "url": "https://www.apple.com", "data": [{ "year": 2017, "totalCount": 0 }, { "year": 2018, "totalCount": 3 }, { "year": 2019, "totalCount": 12 }, { "year": 2020, "totalCount": 3 }, { "year": 2021, "totalCount": 3 } ] }, { "name": "CVS Health", "id": "CVSHealth", "no": 4, "sector": "Health Care", "url": "https://www.cvshealth.com", "data": [{ "year": 2017, "totalCount": 13 }, { "year": 2018, "totalCount": 25 }, { "year": 2019, "totalCount": 26 }, { "year": 2020, "totalCount": 7 }, { "year": 2021, "totalCount": 3 } ] } ] } const result = _.groupBy(_.map(_.flattenDeep(_.map(_.map(_.map(_.keys(objectData), key => { return { data: objectData[key], word: key } }), i => { return { data: i.data.map(j => { return {...j, word: i.word } }) } }), 'data')), k => { return { sector: k.sector, word: k.word, count: _.sumBy(k.data, 'totalCount') } }), 'sector') console.log(result)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

Object 個條目,forEach 遍歷數組,並減少以獲得總數。

 var foo = { "Word 1": [{ "name": "Walmart", "id": "Walmart", "no": 1, "sector": "Retailing", "url": "https://www.walmart.com", "data": [{ "year": 2017, "totalCount": 0 }, { "year": 2018, "totalCount": 2 }, { "year": 2019, "totalCount": 1 }, { "year": 2020, "totalCount": 1 }, { "year": 2021, "totalCount": 0 } ] }, { "name": "Amazon", "id": "amazon", "no": 2, "sector": "Retailing", "url": "https://www.amazon.com/", "data": [{ "year": 2017, "totalCount": 0 }, { "year": 2018, "totalCount": 0 }, { "year": 2019, "totalCount": 0 }, { "year": 2020, "totalCount": 5 }, { "year": 2021, "totalCount": 0 } ] }, { "name": "Apple", "id": "Apple", "no": 3, "sector": "Technology", "url": "https://www.apple.com", "data": [{ "year": 2017, "totalCount": 0 }, { "year": 2018, "totalCount": 0 }, { "year": 2019, "totalCount": 0 }, { "year": 2020, "totalCount": 0 }, { "year": 2021, "totalCount": 0 } ] }, { "name": "CVS Health", "id": "CVSHealth", "no": 4, "sector": "Health Care", "url": "https://www.cvshealth.com", "data": [{ "year": 2017, "totalCount": 0 }, { "year": 2018, "totalCount": 0 }, { "year": 2019, "totalCount": 1 }, { "year": 2020, "totalCount": 0 }, { "year": 2021, "totalCount": 0 } ] } ], "Word 2": [{ "name": "Walmart", "id": "Walmart", "no": 1, "sector": "Retailing", "url": "https://www.walmart.com", "data": [{ "year": 2017, "totalCount": 0 }, { "year": 2018, "totalCount": 0 }, { "year": 2019, "totalCount": 0 }, { "year": 2020, "totalCount": 1 }, { "year": 2021, "totalCount": 0 } ] }, { "name": "Amazon", "id": "amazon", "no": 2, "sector": "Retailing", "url": "https://www.amazon.com/", "data": [{ "year": 2017, "totalCount": 117 }, { "year": 2018, "totalCount": 0 }, { "year": 2019, "totalCount": 0 }, { "year": 2020, "totalCount": 0 }, { "year": 2021, "totalCount": 0 } ] }, { "name": "Apple", "id": "Apple", "no": 3, "sector": "Technology", "url": "https://www.apple.com", "data": [{ "year": 2017, "totalCount": 0 }, { "year": 2018, "totalCount": 3 }, { "year": 2019, "totalCount": 5 }, { "year": 2020, "totalCount": 3 }, { "year": 2021, "totalCount": 9 } ] }, { "name": "CVS Health", "id": "CVSHealth", "no": 4, "sector": "Health Care", "url": "https://www.cvshealth.com", "data": [{ "year": 2017, "totalCount": 0 }, { "year": 2018, "totalCount": 0 }, { "year": 2019, "totalCount": 0 }, { "year": 2020, "totalCount": 0 }, { "year": 2021, "totalCount": 0 } ] } ], "Word 3": [{ "name": "Walmart", "id": "Walmart", "no": 1, "sector": "Retailing", "url": "https://www.walmart.com", "data": [{ "year": 2017, "totalCount": 0 }, { "year": 2018, "totalCount": 0 }, { "year": 2019, "totalCount": 0 }, { "year": 2020, "totalCount": 0 }, { "year": 2021, "totalCount": 0 } ] }, { "name": "Amazon", "id": "amazon", "no": 2, "sector": "Retailing", "url": "https://www.amazon.com/", "data": [{ "year": 2017, "totalCount": 42 }, { "year": 2018, "totalCount": 0 }, { "year": 2019, "totalCount": 0 }, { "year": 2020, "totalCount": 0 }, { "year": 2021, "totalCount": 0 } ] }, { "name": "Apple", "id": "Apple", "no": 3, "sector": "Technology", "url": "https://www.apple.com", "data": [{ "year": 2017, "totalCount": 0 }, { "year": 2018, "totalCount": 3 }, { "year": 2019, "totalCount": 12 }, { "year": 2020, "totalCount": 3 }, { "year": 2021, "totalCount": 3 } ] }, { "name": "CVS Health", "id": "CVSHealth", "no": 4, "sector": "Health Care", "url": "https://www.cvshealth.com", "data": [{ "year": 2017, "totalCount": 13 }, { "year": 2018, "totalCount": 25 }, { "year": 2019, "totalCount": 26 }, { "year": 2020, "totalCount": 7 }, { "year": 2021, "totalCount": 3 } ] } ] }; // Loop over the entries in the object const result = Object.entries(foo).reduce((obj, [key, arr]) => { // Each word in the object has an array so we need to loop over it arr.forEach(value => { // have we seen this sector yet? obj[value.sector] = obj[value.sector] || {}; // get the total for this sector's key obj[value.sector][key] = (obj[value.sector][key] || 0) + value.data.reduce((total, { totalCount }) => total + totalCount, 0); }); return obj; }, {}); console.log(result);

這應該讓你開始(請注意你可能應該創建一個單獨的 function 來循環你的數據,讓它返回總和並在個別情況下調用它)。

objectData.forEach(function(word){
 var a = 0;
 var b = 0;
 // Etc.

  switch(word.sector){
    case "Retailing":
      // Do something;
      break;
    case "Health Care":
      // Do something;
      break;
    default:
      // Do something;
  }

  // Do something with your results
});

暫無
暫無

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

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