i have this api, and i need to create url from what i get from json
https://dog.ceo/api/breeds/list
{"message":["affenpinscher","african","airedale","akita","appenzeller","australian","basenji","beagle","bluetick","borzoi","bouvier","boxer","brabancon","briard","buhund","bulldog","bullterrier","cattledog","chihuahua","chow","clumber","cockapoo","collie","coonhound","corgi","cotondetulear","dachshund","dalmatian","dane","deerhound","dhole","dingo","doberman","elkhound","entlebucher","eskimo","finnish","frise","germanshepherd","greyhound","groenendael","havanese","hound","husky","keeshond","kelpie","komondor","kuvasz","labradoodle","labrador","leonberg","lhasa","malamute","malinois","maltese","mastiff","mexicanhairless","mix","mountain","newfoundland","otterhound","ovcharka","papillon","pekinese","pembroke","pinscher","pitbull","pointer","pomeranian","poodle","pug","puggle","pyrenees","redbone","retriever","ridgeback","rottweiler","saluki","samoyed","schipperke","schnauzer","setter","sheepdog","shiba","shihtzu","spaniel","springer","stbernard","terrier","tervuren","vizsla","waterdog","weimaraner","whippet","wolfhound"],"status":"success"}
and what I need is that, URLs must be like this for each breed
'https://dog.ceo/api/breed/{breed name}/images';
CodePudding user response:
You could use the base url you have there and use string replace function to fill in place holder with the bird breed. Something like this:
const base_url = "https://dog.ceo/api/breed/{breed}/images"
const make_breed_url = (breed) => {
return base_url.replace("{breed}", breed)
}
You could then call
make_breed_url("affenpinscher")
which would return
"https://dog.ceo/api/breed/affenpinscher/images"
CodePudding user response:
like Emiel said , these are basics of javaScript , keep your spirit and learn more i give u simple example below
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arrays</h2>
<p>Multiply every element in the array with 10:</p>
<p id="demo"></p>
<p id="demo2"></p>
<script>
const numbers = [65, 44, 12, 4];
const object ={"message":["affenpinscher","african","airedale","akita","appenzeller","australian"]}
const newArr = numbers.map(myFunction);
const newArrURL = object.message.map(myUrlFunction);
document.getElementById("demo").innerHTML = newArr;
document.getElementById("demo2").innerHTML = newArrURL;
function myFunction(num) {
return num * 10;
}
function myUrlFunction(url) {
return 'https://dog.ceo/api/breed/' url '/images';
}
</script>
</body>
</html>