簡體   English   中英

從對象數組創建一個新對象,並按特定的匹配鍵/值分組

[英]Creating a new object from array of objects and grouping by specific matching key/value

我正在處理的對象數據的示例

var myData = [{
  "name": "John",
  "age": 30,
  "interest": "Baseball"
},
{
  "name": "Bob",
  "age": 21,
  "interest": "Baseball"
},
{
  "name" : "Sally",
  "age" : 29,
  "interest": "Tennis"
}]

我正在嘗試找出按興趣分組的最簡單方法。 我願意使用lodash或下划線,但是我無法獲得最終的結果。

我希望這是輸出:

[{ "Baseball" : [{
                  "name": "John",
                  "age" : 30
                 },
                 {
                  "name" : "Bob",
                  "age" : 21
                 }]
 },
 { "Tennis" : [{
                 "name" : "Sally",
                 "age" : 21
               }]
 }];

基本上,每個興趣都變成一個新的對象鍵,其中包含數組中所有匹配的值。

我在構造此輸出時遇到了麻煩。 任何幫助將不勝感激。 我更喜歡使用lodash /下划線使事情變得很容易。

謝謝!

分組操作可以通過匹配字典(哈希表)中的值來完成。 在JavaScript中,所有對象都是帶有屬性名作為鍵的字典,對於值,我們使用數組。

例如(按下面的“運行代碼段”按鈕查看結果):

 function groupBy( input, propertyName ) { var output = {}; for(var i = 0; i < input.length; i++) { var groupByValue = input[i][propertyName]; if( !(groupByValue in output) ) { output[ groupByValue ] = []; } var dolly = cloneObjectButIgnoreProperty( input[i], propertyName ); output[ groupByValue ].push( dolly ); } return output; } function cloneObjectButIgnoreProperty( value, ignorePropertyName ) { var dolly = {}; var propertyNames = Object.keys( value ); for( var i = 0; i < propertyNames .length; i++ ) { var propertyName = propertyNames[i]; if( propertyName == ignorePropertyName ) continue; dolly[propertyName ] = value[propertyName ]; } return dolly; } var myData = [ { "name": "John", "age": 30, "interest": "Baseball" }, { "name": "Bob", "age": 21, "interest": "Baseball" }, { "name" : "Sally", "age" : 29, "interest": "Tennis" } ]; var groupedByInterest = groupBy( myData, 'interest' ); console.log( "By interest:" ); console.log( groupedByInterest ); var groupedByName = groupBy( myData, 'name' ); console.log( "By name:" ); console.log( groupedByName ); var groupedByAge = groupBy( myData, 'age' ); console.log( "By age:" ); console.log( groupedByAge ); 

您可以為此使用Array.reduce

 var myData = [ { "name": "John", "age": 30, "interest": "Baseball" }, { "name": "Bob", "age": 21, "interest": "Baseball" }, { "name" : "Sally", "age" : 29, "interest": "Tennis" }]; var result = myData.reduce(function(entities, obj) { entities[obj.interest] = (entities[obj.interest] || []).concat({ name: obj.name, age: obj.age }); return entities; }, {}); console.log(result); 

更一般的方法:

 function groupBy(data, key, tFunc) { mapFunc = (typeof tFunc === "function")? tFunc: function(o) { return o }; return (Array.isArray(data)?data:[]).reduce(function(entities, o) { if(o[key]) { entities[o[key]] = (entities[o[key]] || []).concat(tFunc(o)); } return entities; }, {}); } // test code var myData = [ { "name": "John", "age": 30, "interest": "Baseball" }, { "name": "Bob", "age": 21, "interest": "Baseball" }, { "name" : "Sally", "age" : 29, "interest": "Tennis" }]; var result = groupBy(myData, "interest", function(o) { return { name: o.name, age: o.age}}); console.log(result); var result2 = groupBy(myData, "age", function(o) { return o.name}); console.log(result2); 

使用Array.prototype.reduce()函數的解決方案:

 var myData = [{ "name": "John", "age": 30, "interest": "Baseball" }, { "name": "Bob", "age": 21, "interest": "Baseball" }, { "name" : "Sally", "age" : 29, "interest": "Tennis" }], result = myData.reduce(function (r, o) { r[o.interest] = r[o.interest] || []; r[o.interest].push({name: o.name, age: o.age}); return r; }, {}); console.log(result); 

 var myData = [{name:"John",age:30,interest:"Baseball"},{name:"Bob",age:21,interest:"Baseball"},{name:"Sally",age:29,interest:"Tennis"}], result = [], interests = [...new Set(myData.map(v => v.interest))]; interests.forEach(v => result.push({ [v] : [] })); myData.forEach((v,i) => result.forEach((c,i) => Object.keys(c)[0] == v.interest ? result[i][v.interest].push({name: v.name, age: v.age}) : c)) console.log(result); 

暫無
暫無

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

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