Public
Edited
Dec 3, 2022
Importers
4 stars
Insert cell
Insert cell
Insert cell
function getValueByPath(obj, path, seperator = ".") {
const pathArray = typeof path === "string" ? path.split(seperator) : path;
const [first, ...rest] = pathArray;

if (obj == null) return;
if (rest.length === 0) return obj[first];
return getValueByPath(obj[first], rest);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function isObject(a) {
// https://stackoverflow.com/a/8511350
return typeof a === "object" && !Array.isArray(a) && a !== null;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function mapEntries(obj, { key = (k) => k, value = (v) => v } = {}) {
if (typeof obj !== "object") return obj;

return Object.entries(obj).reduce(
(acc, [k, v]) => ({ ...acc, [key(k)]: value(v) }),
{}
);
}
Insert cell
mapEntries(
{ Name: "Jane Doe", City: "Lisbon" },
{ key: (k) => k.toLowerCase(), value: (k) => k.toLowerCase() }
)
Insert cell
Insert cell
function mapKeys(obj, key) {
return mapEntries(obj, { key });
}
Insert cell
mapKeys({ Name: "Jane Doe", City: "Lisbon" }, (k) => k.toLowerCase())
Insert cell
Insert cell
function mapValues(obj, value) {
return mapEntries(obj, { value });
}
Insert cell
mapValues({ Name: "Jane Doe", City: "Lisbon" }, (v) => v.toLowerCase())
Insert cell
Insert cell
function replaceKeys(obj, translationMap) {
return mapKeys(obj, (k) => translationMap.get(k) ?? k);
}
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more