This is the url from which i have to fetch data.I want the frequency of the postIds.How can i do this using the methods (map,filter or reduce).I've done it using a loop .Can it be done in a better way?.please help..
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
fetch('http://jsonplaceholder.typicode.com/comments')
.then(
function(response) {
if (response.status !== 200) {
console.log('Looks like there was a problem. Status Code: ' +
response.status);
return;
}
response.json().then(function(data)
{
var na=[];
for(var i=1;i<=100;i++)
{
var a= data.filter(ab=> {
return ab.postId==i;});
// console.log(a);
na.push({PostId:i,frequency:a.length});
}
console.log(na);
}
)})
.catch(function(err) {
console.log('Fetch Error :-S', err);
});
</script>
</body>
</html>
filter()to create a new array to get it's length iterates the array once for every item in it