Finding Common Items in arrays My goal is to get a list of related statementIds in order of most repeated.

In my database I have stems(Words):

+---------+-----------------+
|  Stem   |   StatementId   |
+---------+-----------------+
| Hello   | [2,46,62]       |
| Good    | [1,123,45]      |
| Morning | [23,2436,12312] |
+---------+-----------------+


My steps:


Get all statementIds

[6, 86],[6],[4],[3, 86],[...] 
Create an empty object, and everytime i see a number,increase its value (or set to 1 if it hasen't been seen before)

 {2: 1, 3: 4, 4: 1, 6: 11, 85: 1, 86: 5, 87: 1, 89: 1}

Sort the object by the attribute and put it in an array of relevancy.

 ["6", "86", "3", "89", "87", "85", "4", "2"]



Full code:

getAllStatementIdFromStem = async (stems: string[]) =&gt; {
    const ids: { [key: number]: number } = {};
    await this.getBulkStem(stems).toArray((stems: Stem[]) =&gt;
      stems.map(({ statements }) =&gt;
        statements.forEach((statementId: number) =&gt; {
          typeof ids[statementId] === "undefined"
            ? (ids[statementId] = 1)
            : ids[statementId]++;
        })
      )
    );
    return Object.entries(ids)
      .sort((a, b) =&gt; a[1] - b[1])
      .reverse()
      .map(x =&gt; x[0]);
  };


Could I be doing something more effectively? I feel like I am doing way too many extra steps.
Overall looks good. I think following improvements can be suggested.

1.Instead of this

typeof ids[statementId] === "undefined"
  ? (ids[statementId] = 1)
  : ids[statementId]++;


How about this for more readability

ids[statementId] = (ids[statementId] || 0) + 1;


2.Instead of doing sort and immediately reverse. You can change sort function (with descending order) so that you can avoid reverse call.