const lastItem = (list) => {
if (Array.isArray(list)) {
return list.slice(-1)[0];
}
if (list instanceof Set) {
return Array.from(list).slice(-1)[0];
}
if (list instanceof Map) {
return Array.from(list.values()).slice(-1)[0];
}
if (!Array.isArray(list) && typeof list === "object") {
return Object.values(list).slice(-1)[0];
}
throw Error(`argument "list" must be of type Array, Set, Map or object`);
};
const l1 = lastItem(["a", "b", "c"]);
const l2 = lastItem(new Set(["d", "e", "f"]));
const l3 = lastItem(
new Map([
["a", "x"],
["b", "y"],
["c", "z"]
])
);
const l4 = lastItem({ a: "x", b: "y", c: "z" });