get last item in map javascript
row.map((rank, i, arr) => {
if (arr.length - 1 === i) {
// last one
} else {
// not last one
}
});
get last item in map javascript
row.map((rank, i, arr) => {
if (arr.length - 1 === i) {
// last one
} else {
// not last one
}
});
Get the last item of Array Set Map object js
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`);
};
// Usage
const l1 = lastItem(["a", "b", "c"]);
// c
const l2 = lastItem(new Set(["d", "e", "f"]));
// f
const l3 = lastItem(
new Map([
["a", "x"],
["b", "y"],
["c", "z"]
])
);
// z
const l4 = lastItem({ a: "x", b: "y", c: "z" });
// z
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us