Reputation: 25
I'm trying put iTunes search to my app, and I met a problem with filter method, I fetched iTunes api to Array, and now I wanna filter that Array by title of song. I'm not understand one thing, why console display:
Uncaught TypeError: songs.filter is not a function
at findSongs (script.js:10)
at <anonymous>:1:1<br><br>
const endpoint = 'https://itunes.apple.com/search?term=jack+johnson&entity=song';
let songs = [];
fetch(endpoint)
.then(blob => blob.json())
.then(data => songs = data);
function findSongs(wordToMatch, songs){
return songs.filter(title => {
const regex = new RegExp(wordToMatch, 'gi');
return title.trackName.match(regex)
});
};
I'm struggling with this problem 2 hours and I got stuck.
any ideas??
thank you very much
Upvotes: 0
Views: 56
Reputation: 19012
The endpoint returns an object which holds all songs details (as an array) in results
property.
E.g. {count : 2, results: [ {}, {} ]}
Try .then(data => songs = data.results);
const endpoint = 'https://itunes.apple.com/search?term=jack+johnson&entity=song';
let songs = [];
fetch(endpoint)
.then(blob => blob.json())
.then(data => songs = data.results);
Upvotes: 3