2017-07-27 72 views
0

我有一個JSON數據庫與對象。每個人都有一個具有特定分配值的屬性:a,b或c。Angular:如何獲得具有特定值的對象的計數?

[ 
    { 
    "id": 1, 
    "category": "a" 
    }, 
    { 
    "id": 2, 
    "category": "b" 
    }, 
    { 
    "id": 3, 
    "category": "c" 
    }, 
    { 
    "id": 4, 
    "category": "a" 
    }, 
    { 
    "id": 5, 
    "category": "a" 
    }, 
    { 
    "id": 5, 
    "category": "b" 
    } 
] 

我想顯示類似:

共有項是:一X ,B X 和c X 。

我知道我必須用objectsinmyjsondatabase.length才能得到總數。

我想知道如何獲得具有特定值的對象的長度(數量)?

+0

如果你正在尋找做計數認爲,這【答案】(https://stackoverflow.com/a/372​​50267/5556177)會幫助你。 – Nehal

回答

-1

您可以輕鬆地指望它有這樣

let objects = [ 
 
    { 
 
     "id": 1, 
 
     "category": "a" 
 
    }, 
 
    { 
 
     "id": 2, 
 
     "category": "b" 
 
    }, 
 
    { 
 
     "id": 3, 
 
     "category": "c" 
 
    }, 
 
    { 
 
     "id": 4, 
 
     "category": "a" 
 
    }, 
 
    { 
 
     "id": 5, 
 
     "category": "a" 
 
    }, 
 
    { 
 
     "id": 5, 
 
     "category": "b" 
 
    } 
 
]; 
 
var filtered = new Array(); 
 
objects.filter(function (t) { 
 
    var found = filtered.some(function (el, index) { 
 
     if (false == (t.category === el.category)) { 
 
      return false; 
 
     } 
 
     filtered[index].count = filtered[index].count + 1; 
 
     return true; 
 

 
    }, filtered); 
 
    if (false === found) { 
 
     filtered.push({category: t.category, count: 1}); 
 
    } 
 
}, filtered); 
 
console.log('There is a total of ' + objects.length + ' items: ' 
 
    + filtered.map(function (t) { 
 
     return t.category + ' x ' + t.count; 
 
    }) 
 
);

0

特定的值對象定義一個函數:

getCount(character) { 
    return this.objects.filter(obj => obj.category === character).length; 
} 

,並調用它: this.getCount('a');

+0

我需要在哪裏聲明這個函數?在我的組件或我的服務? – Jonathan

+0

@jonathan我認爲這應該寫在組件中。服務應該將數據原樣傳遞給組件。但是,是的,你也可以在服務中進行過濾。 –

1

解決您的問題的一種方法是使用map-reduce。這是一個快速解決方案。希望能解決你的問題。

var data = [ 
 
    { 
 
    "id": 1, 
 
    "category": "a" 
 
    }, 
 
    { 
 
    "id": 2, 
 
    "category": "b" 
 
    }, 
 
    { 
 
    "id": 3, 
 
    "category": "c" 
 
    }, 
 
    { 
 
    "id": 4, 
 
    "category": "a" 
 
    }, 
 
    { 
 
    "id": 5, 
 
    "category": "a" 
 
    }, 
 
    { 
 
    "id": 5, 
 
    "category": "b" 
 
    } 
 
]; 
 

 
// First get list of all categories 
 
var categories = data.map(function(x) { return x["category"]; }); 
 

 
// Count no of items in each category 
 
var countByCategories = categories.reduce(function(x, y) { 
 
    if (typeof x !== "object") { 
 
     var reduced = {}; 
 
     reduced[x] = 1; 
 
     reduced[y] = 1; 
 
     return reduced; 
 
    } 
 
    
 
    x[y] = (x[y] || 0) + 1; 
 
    return x; 
 
}); 
 

 
// Final build string that you want 
 
var categoryStrings = []; 
 
for (var category in countByCategories) { 
 
    categoryStrings.push(category + ' x ' + countByCategories[category]); 
 
} 
 

 
var msg = 'There is a total of ' + categories.length + ' items: '; 
 
if (categoryStrings.length > 2) { 
 
    msg = categoryStrings.slice(0, -1).join(', '); 
 
    msg += ' and ' + categoryStrings.slice(-1); 
 
} else { 
 
    msg = categoryStrings.join(', '); 
 
} 
 

 
// print results 
 
console.log(msg);

相關問題