React Native Remove Item From Array Example


Share:
Published By: Admin, Published On: Jun 05, 2021, Category: React Native

Hi Dev,

If you need to see example of how to remove element from array in react native. we will help you to give example of react native remove object from array. i explained simply about react native remove element from array. We will look at example of how to remove object from array in react native.

Here, i will give you three simple example to remove element from array using key and value. so, let's see bellow example how to push object in array in react native app.

Example 1
import React, { Component } from "react";
import {View} from 'react-native';

const MyWebtutsComponent = () => {

  const myArray = [1, 2, 3, 4, 5];

  const index = 1;
  
  myArray.splice(index, 1);
  
  console.log(myArray);

  return (
      <View>
      </View>
  );
};

export default MyWebtutsComponent;
Output
Array [
  1,
  3,
  4,
  5,
]
Example 2
import React, { Component } from "react";
import {View} from 'react-native';


const MyWebtutsComponent = () => {

  const myArray = [1, 2, 3, 4, 5];

  const value = 4;

  myArray.splice(myArray.indexOf(value), 1);
  
  console.log(myArray);

  return (
      <View>
      </View>
  );
};

export default MyWebtutsComponent;
Output
Array [
  1,
  2,
  3,
  5,
]
Example
import React, { Component } from "react";
import {View} from 'react-native';


const MyWebtutsComponent = () => {

  let data = [ 
    { id: 1, name: 'Mike', city: 'philps', state:'New York'},
    { id: 2, name: 'Steve', city: 'Square', state: 'Chicago'},
  ];

    data = data.filter((item) => item.state != 'New York').map(({id, name, city}) => ({id, name, city}));
    console.log(data);

  return (
     
      
  );
};

export default MyWebtutsComponent;
Output
Array [
  Object {
    "city": "Square",
    "id": 2,
    "name": "Steve",
  },
]

It will help you...

Share: