Skip to content

Instantly share code, notes, and snippets.

@ridvanaltun
Created August 22, 2022 08:24
Show Gist options
  • Save ridvanaltun/f3c37cca282a724aae8b7a853ddae9ba to your computer and use it in GitHub Desktop.
Save ridvanaltun/f3c37cca282a724aae8b7a853ddae9ba to your computer and use it in GitHub Desktop.
Extract URLs from objects
const obj = require("./data.json");
function getUrl(obj) {
const ary = [];
helper(obj, ary);
return ary;
}
function helper(item, ary) {
if (typeof item === "string" && isUrl(item)) {
ary.push(item);
return;
} else if (typeof item === "object") {
for (const k in item) {
helper(item[k], ary);
}
return;
}
return null;
}
function isUrl(str) {
if (typeof str !== "string") return false;
return /http|https/.test(str);
}
const urls = getUrl(obj);
console.log(JSON.stringify(urls, " ", "\n"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment