mardi 3 mars 2020

Searching two arrays for matching IDs and if they match, assign a value from a 3rd array

I have the following script. Essentially I have 3 arrays. The data array contains an object with multiple objects within it. I need to grab the ids from each sub object and check to see if an object within tickets has the same ID. If it does, map that ID to a new variable name and then assign that value to the correct index within array2.

I can kind of get this working for the most part, but I am stuck here as this just returns an array of multiple "undefined"s and I am not sure why. I also recognize this could likely be done in a much simpler, or at least concise manner and would love any feedback there.

const _ = require('lodash');
    var data = [
        {
            "access": "360022407234",
            "assignee": "360022307074",
            "audio": "360022407254",
            "autoresponder_upgrade": "360022413913",
            "callback_required": null,
            "camera_surveillance": "360022407274",
            "control": "360022407294",
            "description": "360022306974",
            "due_time": "360022965414",
            "due_time_zone": "360022965434",
            "zd_group": "360022307054",
            "hvac_climate": "360022407314",
            "issue_solved_by_a_reboot": "360022413793",
            "issue_type": "360022407194",
            "lighting": "360022413833",
            "motorized_window_treatment": "360022407334",
            "network": "360022407354",
            "onsite": "360022413753",
            "pd_incident_id": "360022413873",
            "phone_comm": "360022407374",
            "power": "360022407394",
            "priority": "360022307034",
            "proposal_needed": "360022407214",
            "recurring_issue": "360022407454",
            "scheduled": "360022413813",
            "security_alarm": "360022407414",
            "signature_autoresponder_upgrade": "360022413893",
            "status": "360022306994",
            "subject": "360022306954",
            "time_since_last_update": "360022975953",
            "total_time_spent": "360022975933",
            "type": "360022307014",
            "urgency": "360022413773",
            "used_rsm": "360022407434",
            "video": "360022413853",
            "ns_order_id": null
        }
    ];
    var array2 = [
        false,
        false,
        "spontaneous_failure",
        false,
        false,
        false,
        null,
        false,
        false,
        null,
        false,
        null,
        false,
        true,
        false,
        "4:32 PM",
        false,
        "no_on_site",
        "tz-eastern-time-us-canada",
        false,
        "1784",
        "p_normal",
        false,
        "6",
        "solved_by_reboot",
        false,
        false,
        false
    ];
    var tickets = [
        {
            "id": 360022413833,
            "value": false
        },
        {
            "id": 360022407434,
            "value": false
        },
        {
            "id": 360022407194,
            "value": "spontaneous_failure"
        },
        {
            "id": 360022413853,
            "value": false
        },
        {
            "id": 360022407454,
            "value": false
        },
        {
            "id": 360022407214,
            "value": false
        },
        {
            "id": 360022413873,
            "value": null
        },
        {
            "id": 360034298933,
            "value": false
        },
        {
            "id": 360022407234,
            "value": false
        },
        {
            "id": 360022413893,
            "value": null
        },
        {
            "id": 360022407254,
            "value": false
        },
        {
            "id": 360022413913,
            "value": null
        },
        {
            "id": 360022407274,
            "value": false
        },
        {
            "id": 360022407294,
            "value": true
        },
        {
            "id": 360022407314,
            "value": false
        },
        {
            "id": 360022965414,
            "value": "4:32 PM"
        },
        {
            "id": 360022407334,
            "value": false
        },
        {
            "id": 360022413753,
            "value": "no_on_site"
        },
        {
            "id": 360022965434,
            "value": "tz-eastern-time-us-canada"
        },
        {
            "id": 360022407354,
            "value": false
        },
        {
            "id": 360022975933,
            "value": "1784"
        },
        {
            "id": 360022413773,
            "value": "p_normal"
        },
        {
            "id": 360022407374,
            "value": false
        },
        {
            "id": 360022975953,
            "value": "6"
        },
        {
            "id": 360022413793,
            "value": "solved_by_reboot"
        },
        {
            "id": 360022407394,
            "value": false
        },
        {
            "id": 360022413813,
            "value": false
        },
        {
            "id": 360022407414,
            "value": false
        }
    ];
    var access = _.map(data, 'access');
    var assignee = _.map(data, 'assignee');
    var audio = _.map(array1, 'audio');
    var autoresponder = _.map(data, 'autoresponder_upgrade');
    var callback_required = _.map(data, 'callback_required');
    var camera_surveillance = _.map(data, 'camera_surveillance');
    var control = _.map(data, 'control');
    var description = _.map(data, 'description');
    var hvac_climate = _.map(data, 'hvac_climate');
    var issue_solved_by_reboot = _.map(data, 'issue_solved_by_a_reboot')
    var issue_type = _.map(data, 'issue_type');
    var lighting = _.map(data, 'lighting');
    var motorized_window_treatment = _.map(data, 'motorized_window_treatment');
    var network = _.map(data, 'network');
    var onsite = _.map(data, 'onsite');
    var pd_incident_id = _.map(data, 'pd_incident_id');
    var phone_comm = _.map(data, 'phone_comm');
    var power = _.map(data, 'power');
    var priority = _.map(data, 'priority');
    var proposal_needed = _.map(data, 'proposal_needed');
    var recurring_issue = _.map(data, 'recurring_issue');
    var scheduled = _.map(data, 'scheduled');
    var security_alarm = _.map(data, 'security_alarm');
    var signature_autoresponder = _.map(data, 'signature_autoresponder_upgrade');
    var status = _.map(data, 'status');
    var subject = _.map(data, 'subject');
    var type = _.map(data, 'type');
    var urgency = _.map(data, 'urgency');
    var used_rsm = _.map(data, 'used_rsm');
    var video = _.map(data, 'video');
    var ns_order_id = _.map(data, 'ns_order_id');
    var non_time_senstive_reminder = _.map(data, 'non_time_senstive_reminder');
    var array1 = _.map(tickets, 'id');


    for (let i = 0; i < array1.length; i++) {
    if (onsite.includes(array1[i]) == true) {
    var on_site_obj = {'on_site' : array2[i]};
    } else if (issue_type.includes(array1[i]) == true) {
    var issue_type_obj = {'issue_type' : array2[i]};
    }else if (issue_solved_by_reboot.includes(array1[i]) == true) {
    var issue_solved_by_reboot_obj = {'issue_solved_by_reboot' : array2[i]};
    }else if (urgency.includes(array1[i]) == true) {
    var urgency_obj = {'urgency' : array2[i]};
    }else if (scheduled.includes(array1[i]) == true) {
    var scheduled_obj = {'scheduled' : array2[i]};
    }else if (proposal_needed.includes(array1[i]) == true) {
    var proposal_obj = {'proposal_needed' : array2[i]};
    }else if (access.includes(array1[i]) == true) {
    var access_obj = {'access' : array2[i]};
    }else if (audio.includes(array1[i]) == true) {
    var audio_obj = {'audio' : array2[i]};
    }else if (camera_surveillance.includes(array1[i]) == true) {
    var camera_surveillance_obj = {'camera_surveillance' : array2[i]};
    }else if (hvac_climate.includes(array1[i]) == true) {
    var hvac_climate_obj = {'hvac_climate' : array2[i]};
    }else if (control.includes(array1[i]) == true) {
    var control_obj = {'control' : array2[i]};
    }else if (lighting.includes(array1[i]) == true) {
    var lighting_obj = {'lighting' : array2[i]};
    }else if (network.includes(array1[i]) == true) {
    var network_obj = {'network' : array2[i]};
    }else if (motorized_window_treatment.includes(array1[i]) == true) {
    var motorized_window_treatment_obj = {'motorized_window_treatment' : array2[i]};
    }else if (phone_comm.includes(array1[i]) == true) {
    var phone_comm_obj = {'phone_comm' : array2[i]};
    }else if (power.includes(array1[i]) == true) {
    var power_obj = {'power' : array2[i]};
    }else if (video.includes(array1[i]) == true) {
    var video_obj = {'video' : array2[i]};
    }else if (security_alarm.includes(array1[i]) == true) {
    var security_alarm_obj = {'security_alarm' : array2[i]};
    }else if (used_rsm.includes(array1[i]) == true) {
    var used_rsm_obj = {'used_rsm' : array2[i]};
    }else if (recurring_issue.includes(array1[i]) == true) {
    var recurring_issue_obj = {'recurring_issue' : array2[i]};
    }else if (autoresponder.includes(array1[i]) == true) {
    var autoresponder_obj = {'autoresponder' : array2[i]};
    }else if (signature_autoresponder.includes(array1[i]) == true) {
    var signature_autoresponder_obj = {'signature_autoresponder' : array2[i]};
    }else if (pd_incident_id.includes(array1[i]) == true) {
    var pd_incident_id_obj = {'pd_incident_id' : array2[i]};
    }else if (ns_order_id.includes(array1[i]) == true) {
    var ns_order_id_obj = {'ns_order_id' : array2[i]};
    }else if (non_time_senstive_reminder.includes(array1[i]) == true) {
    var non_time_senstive_reminder_obj = {'non_time_senstive_reminder' : array2[i]};

    } else {
    }}

    var finalArray = [on_site_obj,issue_type_obj,issue_solved_by_reboot_obj,urgency_obj,scheduled_obj,proposal_obj,access_obj,audio_obj,camera_surveillance_obj,hvac_climate_obj,control_obj,lighting_obj,network_obj,motorized_window_treatment_obj,phone_comm_obj,power_obj,video_obj,security_alarm_obj,used_rsm_obj,recurring_issue_obj,autoresponder_obj,signature_autoresponder_obj,pd_incident_id_obj,ns_order_id_obj,non_time_senstive_reminder_obj]
    console.log(finalArray);

I am looking for a final output of:

[{access: "value from array2"},{assignee: "value from array2"},{audio: "value from array2"}....]

Aucun commentaire:

Enregistrer un commentaire