Search code examples
arraysstringkql

kql - how to remove all empty strings from an array of strings?


Please tell is it possible to remove all empty strings from an array of strings?

[
    "",
    "Harjumaa",
    "Tallinn",
    "Lastekodu",
    "24",
    ""
]

[
    "Harjumaa",
    "Tallinn",
    "Lastekodu",
    "24"
]

Solution

  • Use set_difference() with the original array and another array with a single empty value.

    Returns a dynamic (JSON) array of the set of all distinct values that are in the first array but aren't in other arrays.

    let list = dynamic([
        "",
        "Harjumaa",
        "Tallinn",
        "Lastekodu",
        "24",
        ""
    ]);
    let list_empty_items_removed = set_difference(list,dynamic([""]));
    print list_empty_items_removed
    
    print_0
    ["Harjumaa","Tallinn","Lastekodu","24"]