2017-06-21 47 views
-2

我有以下陣列:組數組元素超過1場在Javascript

var array = [{id: 123, user: "tester", text: ["wow]}, 
      {id: 123, user: "random", text: ["nice", "cool"]}, 
      {id: 1245, user: "random", text: ["good"]}, 
      {id: 1245, user: "tester", text: ["neat"]}, 
      {id: 123, user: john", text: ["neat", "good", "bye"]}, 
      {id: 12456, user: "tester", text: ["Bye"]}, 
      {id: 1245, user: "random", text: ["wow"]}, 
      {id: 1245, user: "john", text: [{"wow", "nice"]}]; 

比方說,主要用戶是「測試」和其他人是次要的。我想知道主用戶對每個ID有多少文本,以及其他用戶對同一個ID有多少文本。

因此,陣列應該返回:

finalArray = [{id: 123, tester: 1, random: 2, john: 3}, 
       {id: 1245, tester: 1, random: 1, john: 2}, 
       {id: 12456, tester: 1}] 
+3

沒關係。是你的代碼? –

+0

有一個去使用lodash https://lodash.com/docs這可能是你最好的選擇。 你遇到問題,需要幫助進行搜索。 如果找不到答案,請創建一個新帖子。 SO不是一個免費的開發人員服務,有人可以爲您編寫代碼 – Tik

+0

向我們展示您嘗試過的內容。這些數組甚至不是有效的。 https://stackoverflow.com/help/how-to-ask - > https://jsfiddle.net/sheriffderek/Lu1da4a4/ – sheriffderek

回答

0

這裏是代碼片段的問題您指定:

var res = []; 
var obj = {}; 
for(var i in array) { 
    if(array[i].user == 'tester') { 
     if(obj[array[i].id]) 
      obj[array[i].id].tester++; 
     else 
      obj[array[i].id] = {tester: 1}; 
    } 
}    
for(var i in array) { 
    if(obj[array[i].id] && obj[array[i].user] != 'tester') { 
     if(obj[array[i].id][array[i].user]) 
      obj[array[i].id][array[i].user]++; 
     else 
      obj[array[i].id][array[i].user] = 1; 
    } 
} 
for(var i in obj) { 
    var temp = obj[i]; 
    temp.id = i; 
    res.push(temp); 
} 
console.log(res); //output 
0

,您可以利用最新的ES6特點圖/ findIndex和實現這一目標

檢查下面的代碼片段

var arr1 = [{ 
 
    id: 123, 
 
    user: "tester", 
 
    text: ["wow"] 
 
    }, 
 
    { 
 
    id: 123, 
 
    user: "random", 
 
    text: ["nice", "cool"] 
 
    }, 
 
    { 
 
    id: 1245, 
 
    user: "random", 
 
    text: ["good"] 
 
    }, 
 
    { 
 
    id: 1245, 
 
    user: "tester", 
 
    text: ["neat"] 
 
    }, 
 
    { 
 
    id: 123, 
 
    user: "john", 
 
    text: ["neat", "good", "bye"] 
 
    }, 
 
    { 
 
    id: 12456, 
 
    user: "tester", 
 
    text: ["Bye"] 
 
    }, 
 
    { 
 
    id: 1245, 
 
    user: "random", 
 
    text: ["wow"] 
 
    }, 
 
    { 
 
    id: 1245, 
 
    user: "john", 
 
    text: ["wow", "nice"] 
 
    } 
 
]; 
 
let finalResult = []; 
 
arr1.map((item) => { 
 

 
    const resIdx = findFinal(item.id); 
 
    if (resIdx === -1) { 
 
    const obj = { 
 
     id: `${item.id}` 
 
    } 
 
    const userName = item.user 
 
    obj[userName] = 1; 
 
    finalResult.push(obj) 
 
    } else { 
 
    const resObj = finalResult[resIdx] 
 
    const userName = item.user 
 
    if (resObj.hasOwnProperty(userName)) { 
 
     resObj[userName] += 1; 
 
    } else { 
 
     resObj[userName] = 1; 
 
    } 
 
    } 
 
}) 
 
console.log(JSON.stringify(finalResult)) 
 

 
function findFinal(id) { 
 
    return finalResult.findIndex((res) => res.id === id.toString()); 
 
}

希望它可以幫助

1

你可以使用一個哈希表的對象具有相同id並分配text數組的長度到一個屬性與用戶的名稱。

該解決方案在哈希表上使用閉包,並使用Array#reduce獲取具有分組結果的新數組。

var array = [{ id: 123, user: "tester", text: ["wow"] }, { id: 123, user: "random", text: ["nice", "cool"] }, { id: 1245, user: "random", text: ["good"] }, { id: 1245, user: "tester", text: ["neat"] }, { id: 123, user: "john", text: ["neat", "good", "bye"] }, { id: 12456, user: "tester", text: ["Bye"] }, { id: 1245, user: "random", text: ["wow"] }, { id: 1245, user: "john", text: ["wow", "nice"] }], 
 
    result = array.reduce(function (hash) { 
 
     return function (r, a) { 
 
      if (!hash[a.id]) { 
 
       hash[a.id] = { id: a.id }; 
 
       r.push(hash[a.id]); 
 
      } 
 
      hash[a.id][a.user] = a.text.length; 
 
      return r; 
 
     }; 
 
    }(Object.create(null)), []); 
 

 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }