如何计算数组中的重复值并存储在数组中

时间:2016-07-03 12:13:43

标签: javascript arrays data-structures

我有一些对象数组,而且数据很少。我的意见如下:

我的实际代码是:

buildcount: function(store2){
   var datacount = store2.data.items;
    for(var i=0; i<datacount.length; i++){
        var x = datacount[i].data
},

这里从商店我发现x是对象数组,每个对象都有

obj{
    STU: "Study1", SUB: "Subject1", EXL: "Yes"}
}

Object {STU: "Study1", SUB: "Subject1", EXL: "Yes"}
Object {STU: "Study1", SUB: "Subject1", EXL: "Yes"}
Object {STU: "Study2", SUB: "Subject1", EXL: "Yes"}
Object {STU: "Study3", SUB: "Subject1", EXL: "Yes"}
Object {STU: "Study1", SUB: "Subject1", EXL: "Yes"}
Object {STU: "Study3", SUB: "Subject1", EXL: "Yes"}
Object {STU: "Study3", SUB: "Subject1", EXL: "Yes"}
Object {STU: "Study2", SUB: "Subject1", EXL: "Yes"}

我想计算所有唯一元素并将数据存储在数组中。在javascript中。

输出:object {"Study1 : 3", "Study2" : 2, "Study3":4}

6 个答案:

答案 0 :(得分:2)

您可以使用reduce

执行此操作

&#13;
&#13;
var ar =  [{"STU":"Study1","SUB":"Subject1","EXL":"Yes"},{"STU":"Study1","SUB":"Subject1","EXL":"Yes"},{"STU":"Study2","SUB":"Subject1","EXL":"Yes"},{"STU":"Study3","SUB":"Subject1","EXL":"Yes"},{"STU":"Study1","SUB":"Subject1","EXL":"Yes"},{"STU":"Study3","SUB":"Subject1","EXL":"Yes"},{"STU":"Study3","SUB":"Subject1","EXL":"Yes"},{"STU":"Study2","SUB":"Subject1","EXL":"Yes"}]

var result = ar.reduce(function(o, e) {
  return o[e.STU] = (o[e.STU] || 0) + 1, o
}, {});

console.log(result)
&#13;
&#13;
&#13;

答案 1 :(得分:2)

您可以使用filterindexOf的组合:

function arrayUniq(arr) {
    return arr.filter(function(ele, pos) {
        return arr.indexOf(ele) == pos;
    });
}
var array1 =  
['{"STU":"Study1","SUB":"Subject1","EXL":"Yes"}','{"STU":"Study1","SUB":"Subject1","EXL":"Yes"}','{"STU":"Study2","SUB":"Subject1","EXL":"Yes"}','{"STU":"Study3","SUB":"Subject1","EXL":"Yes"}','{"STU":"Study1","SUB":"Subject1","EXL":"Yes"}','{"STU":"Study3","SUB":"Subject1","EXL":"Yes"}','{"STU":"Study3","SUB":"Subject1","EXL":"Yes"}','{"STU":"Study2","SUB":"Subject1","EXL":"Yes"}'];

var unique = arrayUniq(array1);

console.log(unique);

答案 2 :(得分:2)

你可以这样做:

var array = [{STU: "Study1", SUB: "Subject1", EXL: "Yes"},
{STU: "Study1", SUB: "Subject1", EXL: "Yes"},
{STU: "Study2", SUB: "Subject1", EXL: "Yes"},
{STU: "Study3", SUB: "Subject1", EXL: "Yes"},
{STU: "Study1", SUB: "Subject1", EXL: "Yes"},
{STU: "Study3", SUB: "Subject1", EXL: "Yes"},
{STU: "Study3", SUB: "Subject1", EXL: "Yes"},
{STU: "Study2", SUB: "Subject1", EXL: "Yes"}];


function count() {
    var result = {};

    for(var i = 0; i < array.length; ++i){
        for(var el in array[i]){
          if(result[array[i][el]] !== undefined)
            result[array[i][el]] += 1;
         else
            result[array[i][el]] = 1;
      }
    }

    return result;
}

jsfiddle在这里:https://jsfiddle.net/1pfgjaf5/3/

答案 3 :(得分:2)

最好不要有副作用,只使用Reduce。同时保持同事的可读性。我发现最近我使用这段代码作为一个纯函数。我希望它对你也有帮助。 Bin:https://jsbin.com/bajavucowa/edit?js,console

&#13;
&#13;
    var arr = [{STU: "Study1", SUB: "Subject1", EXL: "Yes"},
    {STU: "Study1", SUB: "Subject1", EXL: "Yes"},
    {STU: "Study2", SUB: "Subject1", EXL: "Yes"},
    {STU: "Study3", SUB: "Subject1", EXL: "Yes"},
    {STU: "Study1", SUB: "Subject1", EXL: "Yes"},
    {STU: "Study3", SUB: "Subject1", EXL: "Yes"},
    {STU: "Study3", SUB: "Subject1", EXL: "Yes"},
    {STU: "Study2", SUB: "Subject1", EXL: "Yes"}]
    
    
    var reducer = function(acc, next) {
      if (!acc[next.STU]) {
        acc[next.STU] = 1;
      } else {
        acc[next.STU] = acc[next.STU] + 1;
      }
      return acc;
    };
    
    var result = arr.reduce(reducer, {});

    console.log(result)
&#13;
&#13;
&#13;

答案 4 :(得分:1)

buildcount: function(store2){
   var datacount = store2.data.items;
var uniq = {}
    for(var i=0; i<datacount.length; i++){
        var x = datacount[i].data;
          x.forEach(function(obj) {
       uniq[obj.STU] = uniq[obj.STU] || 0;
    uniq[obj.STU]+=1;
    });
}
},    

答案 5 :(得分:1)

不是最有效的方式,购买易于理解:每次找到新值时,都会在对象中存储值的“哈希”。

var tab = [ {STU: "Study1", SUB: "Subject1", EXL: "Yes"},{STU: "Study1", SUB: "Subject1", EXL: "Yes"},{STU: "Study2", SUB: "Subject1", EXL: "Yes"},{STU: "Study3", SUB: "Subject1", EXL: "Yes"},{STU: "Study1", SUB: "Subject1", EXL: "Yes"},{STU: "Study3", SUB: "Subject1", EXL: "Yes"},{STU: "Study3", SUB: "Subject1", EXL: "Yes"},{STU: "Study2", SUB: "Subject1", EXL: "Yes"}];


var result = {};
var kys;

for(var i=0; i < tab.length ; i++){
var concat = tab[i].STU + tab[i].SUB + tab[i].EXL;



kys = Object.keys(result);

if(kys.indexOf(concat) == -1){
window.console.log("element not found");
result[concat]=concat;

}else{
window.console.log("element found");
}
}
window.console.log(result);

https://jsfiddle.net/6q0w6fsc/