Untitled
Anonymous
plain_text
02/06/2026 6:19 PM
1.0 KB
14
Indexable
export type JSONPrimitive = string | number | boolean | null;
export type JSONValue =
| JSONPrimitive
| JSONObject
| JSONArray;
export interface JSONObject {
[key: string]: JSONValue;
}
export type JSONArray = JSONValue[];
export function getByPath(
value: JSONValue,
path: Array<string | number>
): JSONValue | undefined {
let current: JSONValue = value;
for (const key of path) {
if (
typeof key === "string" &&
current !== null &&
typeof current === "object" &&
!Array.isArray(current)
) {
current = (current as JSONObject)[key];
}
else if (
typeof key === "number" &&
Array.isArray(current)
) {
current = current[key];
}
else {
return undefined;
}
}
return current;
}
Editor is loading...
Leave a Comment