how to delete an object from array in reactjs 1

how to delete an object from array in reactjs

    onDeleteObject = (country) => {
        const newCountries = this.state.countries.filter(element => element.id !== country.id);
        this.setState({ countries: newCountries });

    }

Here is what the above code is Doing:
1. We’re creating a new array called newCountries.
2. We’re filtering the countries array and returning all the countries that don’t have the same id as the country we’re deleting.
3. We’re setting the state of countries to the newCountries array.

Similar Posts