How can I delete the duplicate objects inside Array when I fetch newsApi

S H N

Hi I have some problem fetching specific information based on user choice. I want to fetch newsAPI , inside array there are objects contain countries. The user selects the country he wants by using dropdown, but there is an iterative country when I using map() it passes on all countries with duplicates, I tried to delete the duplicate, but it didn't work + I used reduce() but it didn't work either. Are there any tips and suggestions, maybe there is something I missed.

html:

<html>
 <div class="ui container" id="seachCountry"></div>
</html>

Script:

    <script>
    const apikey = 'https://newsapi.org/v2/top-headlines/sources?category=sports&apiKey=b23fd6aac9c44682abb9580dae85d23f';
        async function getNews(){
            const response = await fetch(apikey);
            const data = await response.json();
            printCards(data)
        }
    
         function printCards(data) {
             const header = document.querySelector('#seachCountry');
                 //Inside  "header.innerHTMI",  I put map() function to pass all items but I couldn't prevent duplicates
                 header.innerHTML += `
                         <select class='ui fluid selection dropdown select' onchange="getCountry(this.value)">
                         <option>select the country</option>
                         ${data.sources.map( countryName => `<option>${countryName.country}</option>`)}
                         </select>`
                         // print all country name using map and dispaly it to user in dropdown WIHT deplicate country name
                     }
    
         async function getCountry(e){
             if ( e !== 'select the country'){
                //Another Fetch based on user choice
                const response = await fetch(`${apikey}&country=${e}`)
                const data = await response.json();
                console.log(data.sources)
    
    
                //It only shows the information of the country in which the user is interested
                document.getElementById("output").innerHTML =  data.sources.map(countryName =>
                    `<div class="ui container card">
                        <div class="content">
                            <h4 class="header">${countryName.name}</h4>
                        </div>
                        <div class="content">
                            <div class="desc">${countryName.description}</div>
                        </div>  
                        <div class="extra content">
                            <span>
                                <a href=${countryName.url} target="_blank" class="ui button btn">Read more</a>
                            </span>
                        </div>
                        </div>`)
             }
        }
        getNews()  
    </script>

Look at this Picture to Clarify the Problem

Nsevens

You can filter the array to have only distinct values by using this function:

source.filter((item, index) => source.indexOf(item) === index);

So, in your case, where you are mapping to a list of options, it will be something like this:

function printCards(data) {
        const header = document.querySelector('#seachCountry');
        const countries = data.sources.map(cn => cn.country);
        const distinctCountries = countries.filter((country, index) => countries.indexOf(country) === index);

        header.innerHTML += `
                        <select class='ui fluid selection dropdown select' onchange="getCountry(this.value)">
                        <option>select the country</option>
                        ${distinctCountries.map(country => `<option>${country}</option>`)}
                        </select>`;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to delete duplicate values of objects inside array?

How can i take the objects inside in the array?

How can I display all the objects of array from fetch in div?

How can I update the array of objects from a fetch response

How can I check if the array of objects have duplicate property values?

How can I fetch duplicate rows

How can I delete special characters inside a nested array?

How can I delete an item inside a nested array with Hooks?

How can I display an array of objects inside a object in React?

How can I reorder an ELEMENT inside of an OBJECT of an array of OBJECTS NOT sort?

How can I fix this for loop to print results inside an Array with Objects?

Fetch request in React: How do I Map through JSON array of object inside of array of objects?

How can I remove duplicate objects and sort?

C++ how can I create an array of objects when there is a constructor?

Looping through an array of objects.. how can I filter out duplicate names and return players in a new array?

How can I can delete item inside array in object of array? javascript

How do I update/delete objects inside this.state array in React

How can I filter an array of objects where an array inside the object includes all items from another array?

How can I order an array with duplicate values?

array of objects are not being fetching when i press the fetch button in react

how do I return an object to the array, when the object is only accessible inside .then() of the fetch api

How can I delete duplicate rows when joining two tables and condition is met

How can I check if the array of objects have duplicate property values with more properties?

How can I check if the array of objects has duplicate property values ​and get the last value that is repeated?

How can I filter an array of objects with one duplicate key and another unique key

How can I find (& de-duplicate) combinations of an array of objects in Javascript?

How to delete an object from state when it is inside an array of objects

How can I fetch data from an array of objects, pass it through an API end point to .fetch() the needed object value using React?

How can I reach the values of an array that is inside an object that is part of an array of objects?