Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To remove item from object in react native, 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 native, 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 native?

React native 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";
import { Text, View } from 'react-native';
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 (
    <View>
      <Text>{JSON.stringify(obj)}</Text>
    </View>
  );
}

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

Output

React Native, remove item from object example
React Native, remove item from object example

Try it yourself