Filters all objects in data
by checking if each element has every required property (props
). Returns an array with
only those objects who have all required properties. props
can be a string or an array of strings. This function is
useful to get rid of "dead" data elements that cannot be displayed or used properly.
Note: Properties (keys
) whose values are either undefined
,null
or ""
are considered as missing properties
and are thus not included the returned array. You can pass additional values to be considered as incomplete data to
emptyValues (as single value or as an array of values).
If you have data like this:
let superheroes = [
{Name: "Storm", EyeColor: "blue"},
{Name: "Stormtrooper", EyeColor: undefined, HairColor: undefined},
{Name: "Superboy", EyeColor: "none", HairColor: "black"},
{Name: "Supergirl", EyeColor: "blue", HairColor: "blond", Height: 165, Weight: 54},
{Name: "Superman", EyeColor: null, HairColor: null},
{Name: "Swarm", EyeColor: "yellow", HairColor: ""}
]
This finds and returns all elements in superheroes
that have the property EyeColor
:
let filteredHeroes = gmynd.deleteIncompleteData(superheroes, "EyeColor");
This is the result:
[
{Name: "Storm", EyeColor: "blue"},
{Name: "Superboy", EyeColor: "none", HairColor: "black"},
{Name: "Supergirl", EyeColor: "blue", HairColor: "blond", Height: 165, Weight: 54},
{Name: "Swarm", EyeColor: "yellow", HairColor: ""}
]
This finds and returns all elements in superheroes
that have both the EyeColor
and HairColor
properties:
let filteredHeroes = gmynd.deleteIncompleteData(superheroes, ["EyeColor", "HairColor"]);
This is the result:
[
{Name: "Superboy", EyeColor: "none", HairColor: "black"},
{Name: "Supergirl", EyeColor: "blue", HairColor: "blond", Height: 165, Weight: 54}
]
This adds the string "none"
to the blacklist of values for the EyeColor
and HairColor
properties:
let filteredHeroes = gmynd.deleteIncompleteData(superheroes, ["EyeColor", "HairColor"], "none");
This is the result:
[
{Name: "Supergirl", EyeColor: "blue", HairColor: "blond", Height: 165, Weight: 54}
]