Arcade If Statement for Multiple Unique Values

4664
14
Jump to solution
03-05-2020 07:17 AM
JaredPilbeam2
MVP Regular Contributor

Using this help page: How To: Group unique values from multiple attribute fields in ArcGIS Pro 

This little Arcade script groups unique values from multiple attribute fields. I created two variables, one for each attribute field. I then grouped unique values from each of the two fields and named it Batteries. It works fine.

var cat = $feature.USER_Categ
var key = $feature.USER_Keywo

if (cat == "Batteries;"){
if (key == "Lead-Acid Batteries;"){
    return "Batteries"}
}‍‍‍‍‍‍‍‍‍‍‍

How would I group multiple unique values? I've tried using multiple if statements, but that didn't seem to work.

0 Kudos
14 Replies
JaredPilbeam2
MVP Regular Contributor

I avoided mentioning my ultimate goal here because it's too broad for the problem at hand, but I guess I will anyway. The reason I need these in groups is because I'm ultimately creating an app from the data with two drop-downs that query by feature layer type-- one for category and one for keyword. Here's where I'm at:

The original data has too many categories to be practical. This is the reason I want to condense the data into groups seen in the finalgroups.csv.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Jared Pilbeam ,

In the middle of the expression you would not put a string test value, but connect directly to the attribute you want to evaluate:

When you run it, the output field will be classified:

You will still end up with 52 categories at the moment:

Expression used for this calculation:

function GetGroup(keyword) {
    var dct_groups = {'Appliance Recycling': 'Air Conditioner;Appliance;Refrigerator;Dishwasher;Dryer;Oven;Stove;Washer;Water Heater',
    'Asbestos Info / Removal Service': 'Asbestos',
    'Automobile & Boat Reuse Recycling': 'Airplane; Automobile;Boat;Car;Recreation Vehicle;RV;Truck',
    'Awards & Recognition': 'Awards (Eco-Award)',
    'Batteries': 'Lead-Acid Batteries;Single Use AA, AAA, C, D, 9-Volt;Batteries - Rechargeable, BackUp;Lead-Acid Batteries;Rechargebale;Disposable Batteries;Rechargeable;Automotive Batteries;Boat Batteries;Lithium Batteries;Button Batteries',
    'Bicycle Reuse Donation': 'Bicycles',
    'Book Recycling': 'Books - Hardcover Recycling;Books - Softcover Recycling',
    'Cellular Telephone Reuse & Recycling': 'Cell Phone - Retail Drop-Off;Cell Phone - Electronic Drop Off',
    'Clean - Up Services': 'Clean-Up of Properties;',
    'Clothing, Linens & Shoe Reuse & Recycling-Drop-Off Locations': 'Clothing;Gym Shoes / Sneakers / HighTops;Linens / Blankets / Tableclothes /  Towels;Textile / Fabrics / Drapes / Curtains;Shoes / Boots;Purse or Handbag',
    'Commercial Hazardous Waste': 'Hazardous Waste Services;Chemical Business Waste',
    'Composting Facilities/Landscape Waste transfer Stations': 'Composting Facilities/Landscape Waste transfer Stations;Landscape Material Recycling',
    'Computer Reuse & Recycling': 'Business Electronic Devices',
    'Construction & Demolition Recycling': 'Asphalt;Brick;Building Construction & Demolition Debris;Carpet;Carpet Padding;Concrete;Dirt;Drywall;Gravel;Padding;Roofing Materials;Shingles;Soil;Stone;Vinyl Siding;Windows;Wood-Untreated;Pallets',
    'Cooking Oil/Grease Recycling & Trap Service': 'Cooking Oil;Grease Recycling;Grease Trap Services',
    'Crayon Recycling': 'Crayons',
    'Curbside Recycling': 'Aerosol Cans (empty) Curbside;Aluminum Can Curbside;Aluminum Foil Curbside;Cardboard & Chipboard Curbside;Curbside Recycling;Glass Bottles & Jars Curbside;Juice Boxes/Drink Pouches Curbside;Paper (magazines, phone bks, etc) Curbside;Plastic #1 (PET) & #2 (HDPE) Containers;Plastic #3, #4, #5, #7, Containers Curbside;Six-Pack Rings Curbside;Tin / Steel Cans (soup,cofffee, etc) Curbside',
    'Demolition Material Recycling & Disposal': 'Construction Material Reuse;Demolition Material Reuse / Recycling',
    'Disposable Batteries': 'Batteries - Single Use AA, AAA, C, D, 9-Volt',
    'Donation': 'Books - Hardcover;Computer Reuse/Donation;Couch;Sofa;Toys',
    'Drum & Cylinder Disposal or Recycling': 'Cylinders;Drums',
    'Dry Cleaners (Green)': 'Dry Cleaning',
    'Electronics': 'Adding Machines;Answering Machines;Calculators;Cameras;Cassettes;CB\'s/Two-way radios;CD Players / Laser Disc Players;CD ROM Drives;CDs, DVDs;Cell Phone - Electronic Drop-Off;Computer Recycling;Computer Recycling Drop-Off;Copy Machine;Cords & Cables;Digital Clocks;DVD Machine;Electronic Mice;Electronics;Fax Machines;Floppy Disks;Hand Held Games;Hard Drives;Joysticks/Game controls;Keyboards;Microwaves;Modems;Monitors;Pagers;Palm Organizers;Paper shredders;Portable Radio; Postage Machines;Printers;Scanner Machines;Speakers/Stereo Systems;String Lights /  Holiday String Lights;Sump Pump;Tape Drives;Telephone;Televisions;Thermometers (digital);Typewriters/Word Processors;UPS Battery Backups;VCR Machine;VHS Tapes;Video Game Players; Zip Drives',
    'Eye Glass Reuse': 'Eye Glasses',
    'Fire Extinguisher Refilling or Recycling': 'Fire Extinguishers',
    'Fluorescent Lights': 'CFLs;Compact Fluorescent Light Bulbs;Fluorescent Lights',
    'Food Donation': 'Food Donation',
    'Furniture & Office Equipment ': 'Book Cases;Cubical Walls;Desks;File Cabinets;Furniture & Office Equipment;Office Chairs',
    'Geothermal Energy': 'Geothermal',
    'Gift Cards': 'Gift Cards (merchant plastic money card)',
    'Green Energy Supplier': 'Energy Supplier',
    'Grocery Bag': 'Grocery Bags',
    'Heating Oil Tank Removal': 'Heating Oil Tank Removal',
    'Household Hazardous Waste (HHW)': 'Aerosol Products;Antifreeze;Automotive Batteries;Automotive Fluid;Boat Batteries;Button Batteries;Cleaning Chemicals;Compact Fluorescent Light Bulbs - HHW Drop-Off;Drain Cleaners;Driveway Sealer;Fertilizers;Fluorescent Light Bulbs;Gasoline-Oil Mix;Hazardous Household Materials;HHW;Household Chemicals;Lawn & garden chemicals;Lithium Batteries;Medication (old or unwanted);Mercury / Mercury Products;Motor Oil;Nail Polish Remover;Oil Filters;Oil-based Paint / Stain / Varnish;Old Gasoline;Paint Thinners;Pesticides;Pool Chemicals;Rechargeable Batteries;Solvents;Thermometers (glass);Used Oil;Medication (old or unwanted);Thermometers (glass);Cough Medicine;Inhalers;Medicated Shampoo;Ointment / Medicated Ointment;Over-the-Counter Medication;Pills, Pharmaceuticals, Medication;Prescription Medication;Sunscreen;Vitamins',
    'Landfill & Transfer Stations': 'Landfills & Transfer Stations;Beverage Carrier Straps;Six-Pack Rings;Plastic #1, PET;Plastic #2, HDPE;Plastic #3, PVC;Plastic #4, LDPE;Plastic #5, PP;Plastic #7, Other Container;Aerosol Cans (empty) Drop-Off;Aluminum Can Recycling Drop-Off ;Aluminum Foil Recycling Drop-Off;Glass Bottles and Jars Drop-Off;Juice Boxes / Drink Pouches  Drop-Off;Tin / Steel Cans (soup, coffee, etc) Drop-Off;',
    'Latex Paint': 'Latex Paint',
    'Lead-base Paint Concerns': 'Lead Paint',
    'Manure': 'Manure',
    'Matress Recycling': 'Matress & Box Springs',
    'Medical Equipment (durable)': 'Canes;Crutches;Portable Toliets;Shower Chairs;Wheelchairs',
    'Medication Drop-Off': 'Cough Medicine;Inhalers;Medicated Shampoo;Ointment / Medicated Ointment;Over-the-Counter Medication;Pills, Pharmaceuticals,Medication;Prescription Medication;Sunscreen;Vitamins',
    'Metal Recycling': 'Scrap Metal;Aluminum / Aluminum Can Scrap;Brass;Copper;Iron;Steel Scrap;',
    'Motor Oil - Bulk': 'Motor Oil in Buk Drums',
    'Motor Oil Recycling (DIY)': 'Motor Oil - DIY',
    'Packaging Materials': 'Packing Peanut ;Styrofoam Peanuts;Cardboard Boxes',
    'Pallet Reuse or Recycling': 'Pallets',
    'Paper Recycling': 'Cardboard;Chipboard;Colored Paper;Construction Paper;Junk Mail;Magazines;Newspaper;Office Paper;Paper Recycling;Shredding Services;Clored Paper',
    'Plastic Carrier Strap Recycling': 'Beverage Carrier Straps;Six-Pack Rings',
    'Plastic Container Recycling': 'Plastic #1, PET;Plastic #2, HDPE;Plastic #3 PVC;Plastic #4, LDPE;Plastic #5, PP;Plastic #7, Other Container',
    'Pop-Tab Recycling': 'Pop-Tab',
    'Printer/Toner Cartridge & Ribbon Recycling': 'Inkjets (Ribbons);Printer/Toner Cartridges',
    'Propane Tank Disposal or Recycling': 'Propane Tanks',
    'Rechargeable Battery': 'Batteries - Rechargeable, BackUp, Lead-Acid;Rechargeable',
    'Recycling and Waste Collection Companies': 'Recycling Collection Company;Garbage Collection Company;Waste Collection Company',
    'Repair Services': 'Sharpening Services',
    'Reuse & Resale Shops': 'Reuse/Resale',
    'Smoke Detectors ': 'Smoke Detectors ',
    'Solar Energy': 'Solar Energy',
    'Styrofoam or Polystyrene Recycling': 'Plastic #6, PS, Polystyrene;Styrofoam',
    'Tire Recycling': 'Tire',
    'Water Conservation': 'Rain Barrels;Rain Garden',
    'Wind Energy': 'Small Wind'}
    
    for (var group in dct_groups) {
        var keywords = dct_groups[group];
        if (Find(keyword, keywords)>-1) {
            return group;
        }       
    }
    return null;
}

// the value you want to evaluate
var test = $feature["USER_Keywo"];


var lst = Split(test, ";");
var groups = [];
for (var i in lst) {
    Console(i)
    var kw = lst[i];
    if (!IsEmpty(kw)) {
        Console(" - kw:" + kw);
        var group = GetGroup(kw);
        if (!IsEmpty(group)) {
            Console(" - group:" + group);
            groups[Count(groups)] = group;
        }
    } else {
        Console(" - item is empty");
    }
}
Console("groups:" + groups);

var dct_groups = {};
for (var i in groups) {
    var group = groups[i];
    if (HasKey(dct_groups, group)) {
        dct_groups[group] += 1;
    } else {
        dct_groups[group] = 1;
    }
}
Console("dct_groups:" + dct_groups);

var cntmax = 0;
var finalgroup = "";
for (var group in dct_groups) {
    var cnt = dct_groups[group];
    if (cnt > cntmax) {
        finalgroup = group;
        cntmax = cnt;
    }
}
Console("finalgroup: " + finalgroup);

return finalgroup;
JaredPilbeam2
MVP Regular Contributor

So you created a "Category" field and did a field calculation? I ran your code in the Expression Builder.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Jared Pilbeam ,

That is correct I did a field calculation on a new field I created. You can also use the expression in the symbology profile or use it in the pop-up if you can't change the schema of the data.

JaredPilbeam2
MVP Regular Contributor

Brilliant. Thank you very much. I'll have to dig deeper tomorrow to see if all the data is represented. I have 62 categories in the finalgroups.csv. And, like you said, there are only 52 here.