Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | 1x | export default function setNestedValue(obj: any, path: string, value: any) { const keys = path.split("."); let current = obj; for (let i = 0; i < keys.length - 1; i++) { const key = keys[i]; const nextKey = keys[i + 1]; Iif (key === "__proto__" || key === "constructor" || key === "prototype") { throw new Error("Invalid key to prevent prototype pollution"); } if (!isNaN(parseInt(nextKey))) { // If next key is a numeric value Iif (!current[key] || !Array.isArray(current[key])) { current[key] = []; } } else { Iif (!current[key] || typeof current[key] !== "object") { current[key] = {}; } } current = current[key]; } const lastKey = keys[keys.length - 1]; Iif ( lastKey === "__proto__" || lastKey === "constructor" || lastKey === "prototype" ) { throw new Error("Invalid key to prevent prototype pollution"); } if (!isNaN(parseInt(lastKey))) { current[parseInt(lastKey)] = value; } else { current[lastKey] = value; } return obj; } |