Return 键,其中值数组包含 AJAX 调用中的搜索文本

Return key where value array includes searched text in AJAX call

我正在使用数据创建一个搜索工具,用户可以在其中搜索涉及法律案件的个人姓名;该工具将输出 table,其中包含个人姓名和角色(以及有关案例的一些其他详细信息),格式如下:

Case name Person Role Status
Case 1 John Smith Lawyer (Claimant) Concluded
Case 2 John Smith Expert (Respondent) Suspended

我正在 AJAX 调用 API 端点来获取此数据,返回对象的示例如下:

    {
        "id": 3,
        "institution": [
            "Institution X"
        ],
        "party": [
            "Test Investments X.Y.",
            "Czech Republic"
        ],
        "chairperson": [
            "Jane Doe",
            "Olive Yew"
        ],
        "arbitratorClaimant": [
            "Hugo First"
        ],
        "arbitratorRespondent": [
            "Greg Arias"
        ],
        "secretary": [
            "Simon Sais"
        ],
        "lawFirmClaimant": [
            "Frank & Stein"
        ],
        "lawyerClaimant": [
            "John Smith"
        ],
        "lawFirmRespondent": [
            "Jen & Tile"
        ],
        "lawyerRespondent": [
            "Anita Bath"
        ],
        "expertFirmClaimant": [
            "Fran & Tick"
        ],
        "expertClaimant": [
            "Laura Biding"
        ],
        "expertFirmRespondent": [
            "Rose & Bush"
        ],
        "expertRespondent": [
            "Peter Owt"
        ],
        "name": "Case 1",
        "caseType": "investment",
        "caseStatus": "concluded",
        "awardDate": "2021-07-04",
        "note": "",
        "chairAppointment": "party agreement",
        "claimantAppointment": "co-arbitrators",
        "respondentAppointment": "other"
    }

为了显示用户在 table 中搜索的个人的角色,我需要一种方法来搜索返回的对象以找到该名称与哪个键相关联。例如,在案例 1 中,John Smith 是索赔人的律师,因此在搜索对象时我需要一个 returns "lawyerClaimant" 的函数。

这是从API请求数据并在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; 
                }

                else if (requestCase==requestConnections) {
                    return null;
                }
                
                else if (individualsArray.includes(requestCase) && requestConnections=='') {
                    var cases = JSON.parse(this.responseText);
                    var caseTable = '<table class="styled-table"><thead><tr><th>Case Name</th><th>Person (Natural)</th><th>Role</th><th>Status</th></tr></thead><tbody>';
                    if (cases.length >= 1) {
                        for (var i in cases.slice(0,3)) {
                            console.log(cases[i]);
                        }

我搜索了其他帖子,发现了一些像这样的函数变体:

function getKeyByValue(object, value) {
  return Object.keys(object).find(key => object[key].includes(value));
}

getKeyByValue(cases[i], requestCase);

但是,当我尝试使用类似的东西时,我收到“.includes 不是函数”TypeError。谁能帮我指引正确的方向?

谢谢!

问题是对象的某些属性不是数组,例如"id": 3。当它尝试在那里使用 .includes() 时,您会收到该错误。所以首先检查该值是一个数组:

function getKeyByValue(object, value) {
  return Object.keys(object).find(key => Array.isArray(object[key]) && object[key].includes(value));
}