Как преобразовать два массива в jquery? - PullRequest
0 голосов
/ 08 мая 2020

У меня две книги. Массив_1 и Массив_2. Мне нужно найти, какие значения из массива_1 найдены и соответствуют значениям массива_2. Каждый раз, когда я нахожу совпадение, устанавливается флажок. Как сравнить два массива?

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

}

Ответы [ 2 ]

1 голос
/ 08 мая 2020

вы можете попробовать следующее:

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})
0 голосов
/ 08 мая 2020

Вы должны перебрать больший массив и проверить, включен ли каждый элемент в другой массив. Это можно сделать несколькими способами или наоборот, но суть в следующем:

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);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...