Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To remove item from object in react js, use delete keyword it will remove key value pair from object only you have to mantion delete keyword with key.

Let’s see how to use delete keyword to remove item from object.

let exampleObject = {
  id: 1,
  name: "infinitbility",
  error: "Feild required"
};
console.log("exampleObject", exampleObject);

// Output
// 'exampleObject' {
//   id: 1,
//   name: 'infinitbility',
//   error: 'Feild required'
// }

delete exampleObject.error;
console.log("exampleObject", exampleObject);

// Output
// 'exampleObject' { id: 1, name: 'infinitbility' }

Today, I’m going to show you How do I remove item from object in react js, as above mentioned, I’m going to use the above-mentioned delete keyword to delete object item.

Let’s start today’s tutorial how do you remove item from object in react js?

React js remove item from object example using Escape characters

Here, we will take sample object state with some data and remove item in useEffect and print object to screen.

import React, { useState, useEffect } from "react";
export default function App() {
  const [obj, setObj] = useState({
    id: 1,
    name: "infinitbility",
    error: "Feild required"
  });

  useEffect(() => {
    let newObj = { ...obj };
    delete newObj.error;
    setObj(newObj);
  }, []);

  return (
    <div className="App">
      <p>{JSON.stringify(obj)}</p>
    </div>
  );
}

I hope it helps you, All the best đź‘Ť.

Output