【jQuery + JSON】で各要素数をカウントする

カテゴリー︎: 【jQuery】

juqeyでjsonデータの各要素数の数を数える

jsonデータ(data.json)

[
{
  "number":"001",
  "tag":"002",
},
{
  "number":"002",
  "tag":"003",
},
{
  "number":"003",
  "tag":"001",
},
{
  "number":"004",
  "tag":"002",
},
{
  "number":"005",
  "tag":"005",
},
{
  "number":"006",
  "tag":"004",
},
{
  "number":"007",
  "tag":"002",
},
{
  "number":"008",
  "tag":"001",
},
{
  "number":"009",
  "tag":"005",
},
{
  "number":"010",
  "tag":"005",
}
]
//各タグごとのカウントを数える
var tag_array = {};
for(var j = 0; j < data.length; j++){
  if(!tag_array[data[j].number]){
    tag_array[data[j].number] = 0;
  }
  tag_array[data[j].number]++;
}
console.log(tag_array);
//結果⇒ {005: 3, 003: 1, 001: 2, 002: 3, 004: 1}
PAGE TOP