javascript-根据内部条件过滤列表

时间:2019-07-04 13:55:37

标签: javascript filtering

我有一个案例,我需要筛选具有特定条件合同(需要批准的合同状态)的代理商的清单

json的样子 代理->有一个载体数组->有一个合约数组(我根据contractStatus进行过滤)

setSelected(): Boolean {
  let result = this._matRadioButton.checked ? this._matRadioButton.checked = false : this._matRadioButton.checked = true
  return result;
}

我的代码

{
      agent_id: "19831225",
      agent_carriers: [
        {
          agent_id: 19831225,
          agent_carrier_id: 10462,
          carrier: "Aetna US Health Care",
          contracts: [
            {
              agent_id: 19831225,
              contractStatus: "APPROVED",

            },{
              agent_id: 19831226,
              contractStatus: "Submitted",

            }
          ]
}

此代码显示了错误的数据,我没有错过 谢谢

更新我的问题 我只需要unfilteredAgents.forEach((agent, agentIndex) => { agent.agent_carriers.forEach((agentCarrier, index) => { const matchedContracts = agentCarrier.contracts.filter(contract => { console.log(searchKeys, " match With ", contract.contractStatus); return searchKeys.includes(contract.contractStatus); }); if (matchedContracts.length > 0) { agentCarrier.contracts = matchedContracts; } else { agent.agent_carriers.splice(index, 1); } }); if(agent.agent_carriers.length === 0){ agent.agent_carriers.splice(index, 1); } }); 的代理商

2 个答案:

答案 0 :(得分:1)

此方法提供了statuses数组,以使其更具动态性和范围。基本上,这种方法会检查contractStatus在数组statuses内的每个对象。

这种方法不会变异原始数组。

let agents = [{  agent_id: "19831225",  agent_carriers: [{    agent_id: 19831225,    agent_carrier_id: 10462,    carrier: "Aetna US Health Care",    contracts: [{      agent_id: 19831225,      contractStatus: "APPROVED"    }]  }]}, {  agent_id: "198312256565",  agent_carriers: [{    agent_id: 198312254545,    agent_carrier_id: 10462,    carrier: "Ele Agent",    contracts: [{      agent_id: 19831225455,      contractStatus: "REJECTED"    },{      agent_id: 19831225455,      contractStatus: "APPROVED"    }]  }]}],
    statuses = ["APPROVED"],
    filtered = agents.filter(({agent_carriers}) => agent_carriers.some(({contracts}) => contracts.every(({contractStatus}) => statuses.includes(contractStatus))));

console.log(filtered);
.as-console-wrapper {min-height: 100%;}

答案 1 :(得分:1)

您也可以过滤内部数组和外部数组,具体取决于内部过滤。

var unfilteredAgents = [{ agent_id: "19831225", agent_carriers: [{ agent_id: 19831225, agent_carrier_id: 10462, carrier: "Aetna US Health Care", contracts: [{ agent_id: 19831225, contractStatus: "APPROVED", }, { agent_id: 19831225, contractStatus: "foo", }] }, { agent_id: 99, agent_carrier_id: 10462, carrier: "Aetna US Health Care", contracts: [{ agent_id: 19831225, contractStatus: "fo", }, { agent_id: 19831225, contractStatus: "foo", }] }] }, { agent_id: "19831225", agent_carriers: [{ agent_id: 19831225, agent_carrier_id: 10462, carrier: "Aetna US Health Care", contracts: [{ agent_id: 19831225, contractStatus: "foo", }] }, { agent_id: 99, agent_carrier_id: 10462, carrier: "Aetna US Health Care", contracts: [{ agent_id: 19831225, contractStatus: "fo", }, { agent_id: 19831225, contractStatus: "foo", }] }] }],
    searchKeys = ["APPROVED"],
    result = unfilteredAgents.filter(agent => {
        var carriers = agent.agent_carriers.filter(agentCarrier => {
            const matchedContracts = agentCarrier.contracts.filter(({ contractStatus }) => searchKeys.includes(contractStatus));
            if (matchedContracts.length) {
                agentCarrier.contracts = matchedContracts;
                return true;
            }
        });
        if (carriers.length) {
            agent.agent_carriers = carriers;
            return true;
        }
    });

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }