Created
August 22, 2022 08:24
-
-
Save ridvanaltun/f3c37cca282a724aae8b7a853ddae9ba to your computer and use it in GitHub Desktop.
Extract URLs from objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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