@nuxtjs/i18n
Version:
Internationalization for Nuxt
36 lines (35 loc) • 865 B
JavaScript
export function pickNested(keys, obj) {
const result = {};
for (const key of keys) {
const value = getNestedValue(obj, key);
if (value !== void 0) {
setNestedValue(result, key, value);
}
}
return result;
}
export function getNestedValue(obj, key) {
const parts = key.split(".");
let current = obj;
for (const part of parts) {
if (current && typeof current === "object" && part in current) {
current = current[part];
} else {
return void 0;
}
}
return current;
}
export function setNestedValue(obj, key, value) {
const parts = key.split(".");
let current = obj;
for (let i = 0; i < parts.length - 1; i++) {
const part = parts[i];
if (!current[part]) {
current[part] = {};
}
current = current[part];
}
const lastPart = parts[parts.length - 1];
current[lastPart] = value;
}