0

I have the following object, Please let me know how to iterate over this object to get the count/length of the property(category) value to know how many times it is repeated.

like:

FIRST is repeated one-time(i.e count or length: 1)
    SECOND is repeated three times(i.e count or length: 3)
    THIRD is repeated three times(i.e count or length: 3)
    FOURTH is repeated one-time (i.e count or length: 1)

var categorycount =

    [{"name":"ABC","vals":[{"description":"first description","category":"FIRST"},
    {"description":"second description","category":"SECOND"},
    {"description":"third description","category":"SECOND"},
    {"description":"fourth description","category":"SECOND"},
    {"description":"fifth description","category":"THIRD"},
    {"description":"sixth description","category":"THIRD"}, 
    {"description":"eighth description","category":"THIRD"},
    {"description":"ninth description","category":"FOURTH"},

    ]}];
    var obj = JSON.stringify(categorycount[0].vals);
    console.log(obj);

jsfiddle.

Dhana
  • 711
  • 3
  • 14
  • 40

3 Answers3

1

@Dhana, I wrote this using forEach loop

const result={};

categorycount[0].vals.forEach(ob => {

    if(result[ob.category]){
    result[ob.category]=result[ob.category]+1;
  } else {
    result[ob.category]=1;
  }
})

console.log(result);
Harmandeep Singh Kalsi
  • 3,315
  • 2
  • 14
  • 26
0

I don't know or it a good way, but it works.

var categorycount = 

[{"name":"ABC","vals":[{"description":"first description","category":"FIRST"},
{"description":"second description","category":"SECOND"},
{"description":"third description","category":"SECOND"},
{"description":"fourth description","category":"SECOND"},
{"description":"fifth description","category":"THIRD"},
{"description":"sixth description","category":"THIRD"}, 
{"description":"eighth description","category":"THIRD"},
{"description":"ninth description","category":"FOURTH"},
]}];
var obj = JSON.parse(JSON.stringify(categorycount[0].vals))

console.log('FIRST: ' + obj.filter(z => z.category == "FIRST").length)
console.log('SECOND: ' + obj.filter(z => z.category == "SECOND").length)
console.log('THIRD: ' + obj.filter(z => z.category == "THIRD").length)
console.log('FOURTH: ' + obj.filter(z => z.category == "FOURTH").length)
Dima Vak
  • 599
  • 5
  • 20
0

What about using a reducer function?

var categorycount = [
  {
    name: 'ABC',
    vals: [
      { description: 'first description', category: 'FIRST' },
      { description: 'second description', category: 'SECOND' },
      { description: 'third description', category: 'SECOND' },
      { description: 'fourth description', category: 'SECOND' },
      { description: 'fifth description', category: 'THIRD' },
      { description: 'sixth description', category: 'THIRD' },
      { description: 'eighth description', category: 'THIRD' },
      { description: 'ninth description', category: 'FOURTH' },
    ],
  },
]

const res = categorycount[0].vals.reduce((acc, cur) => {
  acc[cur.category] = acc[cur.category] ? acc[cur.category] + 1 : 1
  return acc
}, {})

console.log(res)
Gershom Maes
  • 7,358
  • 2
  • 35
  • 55
kjellhaaland
  • 146
  • 2
  • 5