-1

i have an array like given below

OStatus=["hold","approved","rejected","hold","approved","rejected","hold","approved"]

i want something like this in result

Oinfo=["hold":3,"approved":3,"rejected":2].
void
  • 36,090
  • 8
  • 62
  • 107

1 Answers1

1

Assuming that you'd like to produce an object which maps keys which are elements from an input array to values which are the counts of each element, you could use Array.reduce, like so:

Oinfo = OStatus.reduce((acc, x) => ({ ...acc, [x]: (acc[x] || 0) + 1 }), {})
Alex Yuly
  • 180
  • 1
  • 9
  • is it possible to get the count of each types? something like this oinfo[3,3,2] – prabhat katoch Feb 21 '18 at 21:05
  • Given that `OStatus` equals `["hold","approved","rejected","hold","approved","rejected","hold","approved"]`, then my code produces `{"hold":3,"approved":3,"rejected":2}`. Is that what you want? – Alex Yuly Feb 21 '18 at 21:07