-1

I have two ararys. Array_1 and Array_2. I need to find which values from array_1 are found and correspond to the values of array_2. Every single time I find a match, a checkbox is checked. How can I compare two arrays?

var array_1 = ["area_dust_surface", 
               "area_dust_furniture", 
               "area_dust_baseboard",
               "area_dust_ceiling"];

var array_2 = ["area_dust_surface", "area_dust_furniture",
               "area_dust_baseboard","area_dust_ceiling",
               "area_vaccum_carpets", "area_vaccum_mop_floors",
               "area_dust_windows", "area_dust_pictures",
               "area_dust_lamps", "area_dust_mirrors",
               "area_empty_trash", "bath_dust_surface",
               "bath_dust_windows", "bath_dust_cabinets",
               "bath_clean_shower_tubs", "bath_clean_toilets",
               "kitchen_dust_chairs", "kitchen_dust_baseboards",
               "kitchen_dust_fridge_top", "kitchen_clean_out_fridge",
               "kitchen_clean_counters", "kitchen_clean_door_knobs",
               "kitchen_clean_light_switch", "kitchen_clean_sinks",
               "kitchen_clean_microwave", "kitchen_clean_dishwasher",
               "kitchen_clean_kitchen_table",
               "kitchen_vaccum_mop_floor", "kitchen_empty_trash",
               "bed_dust_surface", "bed_dust_furniture_top",
               "bed_dust_surface", "bed_dust_baseboards",
               "bed_dust_door_panels", "bed_dust_windows",
               "laundry_1", "laundry_2", "laundry_3", "laundry_4",
               "laundry_5", "inside_oven_cleaning",
               "inside_refrigerator_cleaning",
               "inside_washer_machine_cleaning",
               "sterilize_washing_machine", "changing_bed_sheets",
               "accessible_windows_cleaning"];

if(jQuery.inArray(array_1, array_2) !== -1){

//do something

}
Grogu
  • 2,097
  • 15
  • 36
  • @freedomn-m: no that's not it at all. I'm not trying to make a union. I'm trying to find matches. – Grogu May 07 '20 at 21:05
  • You're correct - my bad - should have been this answer: https://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript – freedomn-m May 07 '20 at 21:06
  • I don't think he met intersection, but the items in 1 that are in 2 (not that intersect) @freedomn-m – Ali Kleit May 07 '20 at 21:12

2 Answers2

1

you can try the following:

var array_1 = ["area_dust_surface", 
               "area_dust_furniture", 
               "area_dust_baseboard",
               "area_dust_ceiling"];

var array_2 = ["area_dust_surface", "area_dust_furniture",
               "area_dust_baseboard","area_dust_ceiling",
               "area_vaccum_carpets", "area_vaccum_mop_floors",
               "area_dust_windows", "area_dust_pictures",
               "area_dust_lamps", "area_dust_mirrors",
               "area_empty_trash", "bath_dust_surface",
               "bath_dust_windows", "bath_dust_cabinets",
               "bath_clean_shower_tubs", "bath_clean_toilets",
               "kitchen_dust_chairs", "kitchen_dust_baseboards",
               "kitchen_dust_fridge_top", "kitchen_clean_out_fridge",
               "kitchen_clean_counters", "kitchen_clean_door_knobs",
               "kitchen_clean_light_switch", "kitchen_clean_sinks",
               "kitchen_clean_microwave", "kitchen_clean_dishwasher",
               "kitchen_clean_kitchen_table",
               "kitchen_vaccum_mop_floor", "kitchen_empty_trash",
               "bed_dust_surface", "bed_dust_furniture_top",
               "bed_dust_surface", "bed_dust_baseboards",
               "bed_dust_door_panels", "bed_dust_windows",
               "laundry_1", "laundry_2", "laundry_3", "laundry_4",
               "laundry_5", "inside_oven_cleaning",
               "inside_refrigerator_cleaning",
               "inside_washer_machine_cleaning",
               "sterilize_washing_machine", "changing_bed_sheets",
               "accessible_windows_cleaning"];
               
               
const itemsInArray2FromArray1 = array_1.filter(k => array_2.includes(k))

console.log({itemsInArray2FromArray1})
Ali Kleit
  • 3,069
  • 2
  • 23
  • 39
0

You should iterate through the larger of the arrays, and check if each item is included in the other array. This can be done in multiple ways or possibly reversed, but the gist of it is:

const array_1 = ["area_dust_surface",
  "area_dust_furniture",
  "area_dust_baseboard",
  "area_dust_ceiling"
];

const array_2 = ["area_dust_surface", "area_dust_furniture",
  "area_dust_baseboard", "area_dust_ceiling",
  "area_vaccum_carpets", "area_vaccum_mop_floors",
  "area_dust_windows", "area_dust_pictures",
  "area_dust_lamps", "area_dust_mirrors",
  "area_empty_trash", "bath_dust_surface",
  "bath_dust_windows", "bath_dust_cabinets",
  "bath_clean_shower_tubs", "bath_clean_toilets",
  "kitchen_dust_chairs", "kitchen_dust_baseboards",
  "kitchen_dust_fridge_top", "kitchen_clean_out_fridge",
  "kitchen_clean_counters", "kitchen_clean_door_knobs",
  "kitchen_clean_light_switch", "kitchen_clean_sinks",
  "kitchen_clean_microwave", "kitchen_clean_dishwasher",
  "kitchen_clean_kitchen_table",
  "kitchen_vaccum_mop_floor", "kitchen_empty_trash",
  "bed_dust_surface", "bed_dust_furniture_top",
  "bed_dust_surface", "bed_dust_baseboards",
  "bed_dust_door_panels", "bed_dust_windows",
  "laundry_1", "laundry_2", "laundry_3", "laundry_4",
  "laundry_5", "inside_oven_cleaning",
  "inside_refrigerator_cleaning",
  "inside_washer_machine_cleaning",
  "sterilize_washing_machine", "changing_bed_sheets",
  "accessible_windows_cleaning"
];

function array_intersect(source, target) {
  const output = [];
  for (const a of source) {
    if (!output.includes(a) && target.some(i => i === a)) {
      output.push(a);
    }
  }
  return output;
}

for (const exists of array_intersect(array_2, array_1)) {
  console.log('needs to be checked ON:', exists);
}
casraf
  • 21,085
  • 9
  • 56
  • 91