I have urls like this:
data:
{
"zip": 442,
"code": "AG",
"capital": "london",
"currency": {
"code": "XCD",
"name": "East"
"link": "https://en.wikipedia.org/wiki/",
"url": "https://en.wikipedia.org/"
}
I want to write an if statement that checks if the object starts with a 'https:' and then tell it what to do.
This is what I have, but it doesn't make my urls clickable or returns its data.
if (jsonObject.startsWith("http://")) {
const text = document.createElement("a");
k.textContent = jsonObject;
k.setAttribute("href", jsonObject);
j.appendChild(k);
You're close I believe, you just aren't accessing the actual values inside of the object.
If you want to check the Object's properties values and if they start with http/https you can loop through the object and use the same check.
jsonObject.values.forEach((value) => {
if(value.startsWith("https://") {
const text = document.createElement("a");
k.textContent = value;
k.setAttribute("href", value);
j.appendChild(k);
}
});