JSFiddle - React, Tailwind, and code Playground

JavaScript

var data = [{
  "_id": "2",
  "parentId": "1",
  "topLevelId": "1",
  "text": "<p>reply to comment</p>",
}, {
  "_id": "3",
  "parentId": "2",
  "topLevelId": "1",
  "text": "<p>reply to reply to comment</p>",
}, {
  "_id": "1",
  "parentId": "thisPostId",
  "topLevelId": "1",
  "text": "<p>comment</p>",
}, {
  "_id": "4",
  "parentId": "3",
  "topLevelId": "1",
  "text": "<p>reply to reply to reply to comment</p>",
}]

var searchId = '2';

function getTLID(searchId) {
  return data.filter(function(el) {
    return el._id === searchId;
  })[0].topLevelId;
}

    function getIdArray(searchId) {
      var tlid = getTLID(searchId);
      return data.reduce(function (p, c) {
        var matchSearchId = +c.parentId >= +searchId || c._id === searchId;
        if (c.topLevelId === tlid && matchSearchId) p.push(c._id);
        return p;
      }, []).sort();
    }

console.log(getIdArray('1'))
console.log(getIdArray('2'))
console.log(getIdArray('3'))
console.log(getIdArray('4'))