I have a project where I am handling data to develop a search tool that allows users to search for the name of an individual involved in a legal case. The tool will then display a table with the individual's name, role, and other case details in a specific format:
Case Name | Person | Role | Status |
---|---|---|---|
Case 1 | John Smith | Lawyer (Claimant) | Concluded |
Case 2 | John Smith | Expert (Respondent) | Suspended |
I am using AJAX calls to fetch this data from the API endpoint. An example of the returned object is as follows:
{
"id": 3,
"institution": [
"Institution X"
],
"party": [
"Test Investments X.Y.",
"Czech Republic"
],
...
To display the role of the searched individual in the table, I need to search through the returned object to find the key associated with the name. For example, in Case 1, John Smith was a lawyer for the Claimant, so I need a function that returns "lawyerClaimant" when searching through the object.
Here is the beginning of the function to request data from the API and display it in the table:
function requestCases() {
var requestCase = document.getElementById('case-search-input').value;
var requestConnections = document.getElementById('connections-search-input').value;
var xhr = new XMLHttpRequest();
xhr.open('GET', `api/case-list/?first-person=${requestCase}&second-person=${requestConnections}`, true);
xhr.onload = function() {
if(this.status == 200) {
if (requestCase=='' && requestConnections=='') {
return null;
}
...
I have come across variations of functions like this:
function getKeyByValue(object, value) {
return Object.keys(object).find(key => object[key].includes(value));
}
getKeyByValue(cases[i], requestCase);
However, when I try to use something similar, I encounter a ".includes is not a function" TypeError. Can anyone provide guidance on how to proceed?
Thank you!