Search code examples
javascriptreactjsaxioslocalhostjson-server

How can I delete the data from a json server in Reactjs


Link of the JSON data https://jsoneditoronline.org/#left=cloud.e09cb2f612284a47a28c71a2c813da80
How can I delete subjectname from JSON data my code is here http://localhost:3003/CourseList is my local JSON server

 const deleteSub = async id => {
        try{
          const data = await axios.get("http://localhost:3003/CourseList")
          //How should I map and delete the subname ???? 
        }
        catch(err){
          console.log(err.message)
        }
      };

Solution

  • You can use .filter method to do this

    const deleteSub = async id => {
            try{
              const data = await axios.get("http://localhost:3003/CourseList")
              const filteredData = data.filter(d => d.id !== id)
              console.log(filteredData)
            }
            catch(err){
              console.log(err.message)
            }
    };