Home » Jquery » jquery – Is there any way of selecting only certain key value pair of the dictionary using javascript?-Exceptionshub

jquery – Is there any way of selecting only certain key value pair of the dictionary using javascript?-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I have a dictionary called Data. I am taking the user input and inserting that input in the following dictionary.

I have various if conditions according to which I want only specific data to be shown in the downloaded data. For instance I have one condition that if I have only 1 controller then I need to show only the ones which are ending with _A in the below given names. Similarly I have other conditions, so instead of manually entering the edited dictionary for every condition, is there a way by which I can simply filter a dictionary according to the condition?

I am attaching a sample code for the same. Any help would be much appreciated.

let data = {
    "   PDU:": {
      "PDU_IP": ['      PDU_IP', ' ' + oForm.ip.value, "    #PDU IP", ' ' + '\n'],
      "PDU_LEFT": ['        PDU_LEFT', [oForm.lo.value], " # left_outlet(s) ", '    ' + '\n'],
      "PDU_RIGHT": ['       PDU_RIGHT', [oForm.ro.value], " # right_outlet(s) ", '  ' + '\n']
    },
    "   \n  Controller: # Controller settings ": {

      "Controller_ID#1": ["     -  # Controller A for RBOD/EBOD"],
      "Netmask IP_A": ["            MC_IP", ' ' + document.getElementById('mc_ip').value, '  # RBOD MC IP', '' + '\n'],
      "MC_Netmask_A": ["            MC_Netmask", ' ' + document.getElementById('netmask_ip1').value, '  # RBOD MC Netmask', '' + '\n'],
      'MC_Gateway_A': ['            MC_Gateway', ' ' + document.getElementById('gateway_ip1').value, '  # RBOD MC Gateway', '' + '\n'],
      'MC_A': ['            MC', ' ' + document.getElementById('rbod_mc1').value, '  # RBOD MC port', '' + '\n'],
      'SC_A': ['            SC', ' ' + document.getElementById('rbod_sc1').value, '  # RBOD SC port', '' + '\n'],
      'FU_A': ['            FU', ' ' + document.getElementById('rbod_fu1').value, '  # RBOD FU port', '' + '\n'],
      'EC_A': ["            EC", ' ' + document.getElementById('rbod_ec1').value, '  # EC port on RBOD or GEM port on JBOD', '' + '\n'],
      'Controller_ID#2': ["         ID", ' ' + document.getElementById('Controller_ID1').value, "   #Controller ID, A or B", '' + '\n'],
      'Controller_ID#3': ["         ID", ' ' + document.getElementById('Controller_ID1').value, "   #Controller ID, A or B", '' + '\n'],
      'Controller_ID#4': ['\n' + "      -  # Controller B for RBOD/EBOD"],
      //"HBA_Ports_B":["            HBA_Ports",oForm.hba_ports.value,'  # SAS/Fiber Channel ',''+'\n'],
      "Netmask IP_B": ["            MC_IP", ' ' + document.getElementById('mcip').value, '  # RBOD MC IP', '' + '\n'],
      "MC_Netmask_B": ["            MC_Netmask", ' ' + document.getElementById('netmaskip').value, '  # RBOD MC Netmask', '' + '\n'],
      'MC_Gateway_B': ['            MC_Gateway', ' ' + document.getElementById('gatewayip.value'), '  # RBOD MC Gateway', '' + '\n'],
      'MC_B': ['            MC', ' ' + document.getElementById('rbodmc').value, '  # RBOD MC port', '' + '\n'],
      'SC_B': ['            SC', ' ' + document.getElementById('rbodsc').value, '  # RBOD SC port', '' + '\n'],
      'FU_B': ['            FU', ' ' + document.getElementById('rbodfu').value, '  # RBOD FU port', '' + '\n'],
      'EC_B': ["            EC", ' ' + document.getElementById('rbodec').value, '  # EC port on RBOD or GEM port on JBOD', ' ' + '\n'],
      'Controller_ID#4': ["         ID", ' ' + document.getElementById('Controller_ID').value, "    #Controller ID, A or B", '' + '\n']
    }

What I am looking for is just this much should be loaded

if ((document.getElementById("ip1").disabled || document.getElementById("ip2").disabled) && oForm.ctrls.value == 1) {
  let data = {
    "   PDU:": {
      "PDU_IP": ['      PDU_IP', ' ' + oForm.ip.value, "    #PDU IP", ' ' + '\n'],
      "PDU_LEFT": ['        PDU_LEFT', [oForm.lo.value], " # left_outlet(s) ", '    ' + '\n'],
      "PDU_RIGHT": ['       PDU_RIGHT', [oForm.ro.value], " # right_outlet(s) ", '  ' + '\n']
    },
    "   \n  CONTROLLER: # Controller settings ": {
      "Controller_ID#1": ["     -  # Controller A for RBOD/EBOD"],
      "Netmask IP_A": ["            MC_IP", ' ' + document.getElementById('mc_ip').value, '  # RBOD MC IP', '' + '\n'],
      "MC_Netmask_A": ["            MC_Netmask", ' ' + document.getElementById('netmask_ip1').value, '  # RBOD MC Netmask', '' + '\n'],
      'MC_Gateway_A': ['            MC_Gateway', ' ' + document.getElementById('gateway_ip1').value, '  # RBOD MC Gateway', '' + '\n'],
      'MC_A': ['            MC', ' ' + document.getElementById('rbod_mc1').value, '  # RBOD MC port', '' + '\n'],
      'SC_A': ['            SC', ' ' + document.getElementById('rbod_sc1').value, '  # RBOD SC port', '' + '\n'],
      'FU_A': ['            FU', ' ' + document.getElementById('rbod_fu1').value, '  # RBOD FU port', '' + '\n'],
      'EC_A': ["            EC", ' ' + document.getElementById('rbod_ec1').value, '  # EC port on RBOD or GEM port on JBOD', '' + '\n'],
      'Controller_ID#2': ["         ID", ' ' + document.getElementById('Controller_ID1').value, "   #Controller ID, A or B", '' + '\n'],
    }
  };
How to&Answer: