Does anyone have a solution for resetting the ID numbers in a flatlist after removing an item from it?

As the title suggests, I am facing an issue with the following scenario:

{ id: '1', name: 'one' },
    { id: '2', name: 'two' },
    { id: '3', name: 'three' },
    { id: '4', name: 'four' },

After removing the item with id '2', I want id 3 to become 2 and id 4 to become 3 so that the flatlist looks like this:

{ id: '1', name: 'one' },
        { id: '2', name: 'three' },
        { id: '3', name: 'four' },

Here is the code snippet I am using:

export default function Listdata({ route }) {
    const [itemData, newItem] = React.useState([]);

    const [itemState, setItemState] = React.useState(itemData);
    const [idmoi, incr,] = React.useState(1);
    const [textNhapVao, setTextNhapVao] = React.useState('');


    const tinhToanId = (t) => {
        var idNew = [itemData.id];
        incr(idNew - 1);
    }


    const themItem = () => {
        var arrayMoi = [...itemData, { id: idmoi, name: textNhapVao }];
        incr(idmoi + 1)
        console.log('idddd')
        console.log(idmoi)
        setItemState(arrayMoi);
        newItem(arrayMoi);
    }
    <View>

    </View>

    const keyboardVerticalOffset = Platform.OS === 'ios' ? 40 : 0

    const xoaItem = (IItem) => {
        console.log('routeeee')
        console.log(route.params.paramKey)
        setItemState(prevItemState => prevItemState.filter((_item, _Index) => _Index !== IItem));
    }
    return (
        <Container style={styles.container}>
            <View style={{
                alignItems: 'center',
                justifyContent: 'center',
                borderBottomWidth: 1,
                borderColor: '#d7d7d7',
            }}>
                <Text style={{ fontWeight: 'bold', fontSize: 30, color: 'green' }}>Xin Chào {route.params.paramKey}</Text>
            </View>
            <FlatList
                data={itemState}
                keyExtractor={(item, index) => index}
                renderItem={({ item, index }) => (
                    <View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
                        <View style={{ marginLeft: 20 }}>
                            <Text style={{ fontSize: 30, color: 'red' }} >{item.id}{'\n'}{item.name}</Text>
                        </View>
                        <View style={{ justifyContent: 'center', marginRight: 20 }}>
                            <TouchableOpacity
                                style={{
                                    width: '100%',
                                    backgroundColor: 'red',
                                }}
                                activeOpacity={0.7}
                                onPress={() => xoaItem(index)}
                            >
                                <IconFE name='trash-2' size={30} style={{ color: 'orange' }} />
                            </TouchableOpacity>
                        </View>
                    </View>
                )}
            />
            <View
                style={{
                    position: 'relative', height: 50,
                    borderTopWidth: 1,
                    borderColor: '#d7d7d7',
                }}>
                <KeyboardAvoidingView enabled behavior={Platform.OS === "ios" ? "padding" : null} keyboardVerticalOffset={keyboardVerticalOffset} >
                    <View
                        style={{
                            alignItems: 'center', position: 'relative',
                            flexDirection: 'row',
                            justifyContent: 'space-between',
                            marginLeft: 20,
                            marginRight: 20,
                        }}>
                        <Input
                            onChangeText={data => setTextNhapVao(data)}
                            placeholder='Nhập Vào Đây'></Input>
                        <TouchableOpacity
                            title="Thêm"
                            onPress={themItem}>
                            <IconFE name='check-square' size={30} style={{ color: 'blue' }} />
                        </TouchableOpacity>
                    </View>
                </KeyboardAvoidingView>
            </View>
        </Container>
    )
}

Here is a screenshot of my flatlist for reference:

Answer №1

One method is to create a function that can remove an object at a specified index.

This function will extract the removed object from the array, retrieve its id, and then iterate through the array from that index onwards, updating all subsequent id properties.

const
    remove = (array, index) => {
        let removed = array.splice(index, 1);

        if (!removed.length) return array;
        let id = +removed[0].id;
        while (index < array.length) array[index++].id = (id++).toString();
        return array;
    },
    data = [{ id: '1', name: 'one' }, { id: '2', name: 'two' }, { id: '3', name: 'three' },  { id: '4', name: 'four' }];

remove(data, 1);

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Answer №2

Remove a specific element from an array using the splice method by identifying its index

See a demo here:

import React, { Component } from 'react';
import {View, Text, StyleSheet, Image, TouchableOpacity, Dimensions, FlatList} from 'react-native';

export default class SupervisorDashboard extends Component<Props> {

 constructor(props) {
    super(props);
    this.state = { 
      userData:[
                {id:1, name:"One"},
                {id:2, name:"Two"},
                {id:3, name:"Three"},
                {id:4, name:"Four"},
                {id:5, name:"Five"},
                {id:6, name:"Six"},
              ]
      
     
    }    
  }

  componentDidMount(){
    
  }


  removeItem(index){
    this.state.userData.splice(index, 1);
    var array = []
    // console.log(JSON.stringify(this.state.userData))
    for(var i=0; i< this.state.userData.length; i++){
      var eachElement = {id: (i+1), name: this.state.userData[i].name }
      array.push(eachElement)
    }
    console.log(JSON.stringify(array)) 

    this.setState({}) 
  }

  renderItem(item, index){
    // console.log(item.id) 
    return(
      <View style={{height:60, width:'90%', marginTop:10, marginLeft:'5%', marginRight:'5%', flexDirection:'row', justifyContent:'space-between', alignItems:'center', borderRadius:10, borderWidth:1, borderColor:'#ececec', padding:10}}>
        <Text>{item.id}</Text>
        <Text>{item.name}</Text>
        <TouchableOpacity onPress={()=>{this.removeItem(index)}} style={{marginRight:10, backgroundColor:'grey', height:'100%', justifyContent:"center", borderRadius:10, padding:10}}>
            <Text>Click to remove</Text>
        </TouchableOpacity>
      </View>
    )
  }
 
  
  render(){
    return(
      <View style={{flex:1, backgroundColor:'white'}}>
                <FlatList
                data={this.state.userData}
                renderItem={({ item, index })=>this.renderItem(item, index)}
                keyExtractor={item => item.id}
              />
        
      </View>
    );
  }

}

const styles = StyleSheet.create({
  background: {
    backgroundColor: 'red'
  }
});

Answer №3

From what I can gather, it seems like the id values are not in a continuous sequence (1,2,3...). If that's the case, there may be a different approach to handling the data without needing to mutate the id. Instead, you could potentially use the indices of the array. Here's a proposed algorithm:

  • Identify the index of the element to remove,
  • Map each element n to n+1 for indices greater than the remove index,
  • Remove the last element (which is the item to be removed, pushed to the end).

Here's an example of the code implementation:

const data = [
    { id: '1', name: 'one' },
    { id: '2', name: 'two' },
    { id: '5', name: 'five' },
    { id: '6', name: 'six' }]
    
    
const removeFromArr = (arr, name) => {
  const removeIdx = arr.findIndex(e => e.name === name)
  return arr.map((e, i, a) => removeIdx <= i ? ({...e, name: a?.[i + 1]?.name}) : e) // map element n to n+1 if higher than remove idx
            .slice(0, arr.length - 1) // remove last item
}
 
const newData = removeFromArr(data, "two")
 
console.log(newData)
.as-console-wrapper { max-height: 100% !important; top: 0; }

Answer №4

To obtain the desired outcome is quite simple

  • Locate the index of the element you wish to delete utilizing the findIndex method
  • Subsequently, split the array into two parts
  • Increment the second part of the array after converting it to an integer with int
  • Concatenate the array together
  • Remember to handle the scenario in which the element is not present in the arr

const arr = [
  { id: "1", name: "one" },
  { id: "2", name: "two" },
  { id: "3", name: "three" },
  { id: "4", name: "four" },
];

function adjustIds(arr) {
  return arr.map((o) => {
    return { ...o, id: `${parseInt(o.id) - 1}` };
  });
}

const id = "2";
const elementToRemoveIndex = arr.findIndex((o) => o.id === id);

const finalResult =
  elementToRemoveIndex !== -1
    ? [
        ...arr.slice(0, elementToRemoveIndex),
        ...adjustIds(arr.slice(elementToRemoveIndex + 1)),
      ]
    : [...arr];

console.log(finalResult);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Programmatically setting properties for elements

I have a question about how to programmatically add a prop to a component in my React project. Here is the scenario: In the render() method, I have the following code snippet: <TextField name="password" va ...

Tips for extracting a specific attribute from an array of objects using both react and JavaScript while considering various conditions

I have a set of objects structured like this: const obj_arr = [ { id: '1', jobs: [ { completed: false, id: '11', run: { id: '6&apos ...

Utilize the atan2() function to rotate a div so that it follows the movement of the mouse

I am trying to create an effect where the mouse aligns with the top of a div and the div rotates as the mouse moves. The rotation should happen when the top part of the div is in line with the mouse cursor. My goal is to achieve this using the atan2 functi ...

Ensuring compatibility of peerDependencies through devDependencies in npm3

With the recent update to NPM 3, automatic resolving of peer dependencies has been removed. This poses a challenge when developing a plugin/library for consumption by another application. If the underlying library uses peerDependencies, it requires manual ...

Utilize pivot to manage user roles and permissions in ExpressJS application using Mongoose

My user schema is structured as shown below const userSchema = mongoose.Schema({ username: { type: String, required: true, }, first_name: { type: String, required: true, }, last_name: { type: Stri ...

Displaying various images within a bootstrap modal window

I'm facing an issue with my gallery. It contains small images, and when a user clicks on a thumbnail, a modal should open with the full-size image. The problem is that even though I have the full-size images stored in the "/uploads/" folder and the th ...

Ways to categorize specific columns based on certain criteria

In the code snippet below, I am working with a data grid in premium version. There is a boolean field that determines whether to display the data grouped or inline. If the boolean is true, I want to show the expand group, otherwise display it inline withou ...

Implementing TestCafe with a Rails backend and utilizing fixtures data for testing

Currently, I am involved in a Rails project that utilizes RSpec for testing. We rely on various Rails fixture data to test our UI under different conditions. Recently, I came across TestCafe for handling functional UI testing, and I find it quite intrigui ...

Encountering an Ajax Issue with Laravel 5.4

I encountered the following error: "{"status":"error","msg":"Category was not created"}" Below is my Controller Function where I execute the action : function create_category(Request $request){ if($request->ajax()){ $c ...

What is the purpose of assigning controller variables to "this" in AngularJS?

Currently, I am analyzing an example in CodeSchool's "Staying Sharp with Angular" course in section 1.5. Here is the code snippet: angular.module('NoteWrangler') .controller('NotesIndexController', function($http) { var contro ...

What's causing the unexpected rendering outcome in Three.js?

Here is a mesh created in Blender: https://i.sstatic.net/KBGM5.jpg Upon loading it into Three.js, I am seeing this result: https://i.sstatic.net/PCNQ8.jpg I have exported it to .obj format and ensured all faces are triangulated. I am not sure why this is ...

Trouble with displaying ChartsJS Legend in Angular11

Despite thoroughly researching various documentation and Stack Overflow posts on the topic, I'm still encountering an odd issue. The problem is that the Legend for ChartsJS (the regular JavaScript library, not the Angular-specific one) isn't appe ...

Blurry text and icons in Bootstrap 3

Does anyone else notice a strange display issue with Bootstrap 3 fonts and glyphicons? It seems like the bitmaps and fonts are appearing blurry on desktops when using Firefox and Chrome, but everything looks fine on mobile devices. I've attached an ex ...

Empty screen appears when "npm run serve" command is executed following the build process

I am currently utilizing Material-ui. Following the project build with npm run build, I encounter a blank page when running npm run serve. I attempted to set homepage: "./" in the package.json as suggested here, however, it still displays a blank ...

JavaScript substring() function in clone is experiencing an error

I am currently working on a JavaScript function that determines whether a specific substring is present in a larger main string. For instance, if the main string is "111010" and the substring is "011," the expected result should be false since the substr ...

Even after assigning the class "img-fluid" to my image, it still fails to properly adjust to the screen size

I added the class "img-fluid" to my image in Bootstrap, but it's not fitting on my webpage. What should I do? <img src="images\406201.jpg" class="img-fluid" alt="..."> <div style="margin-top: 0p ...

What is the best way to execute a JavaScript file with npm scripts?

Trying to use npm but encountering some issues. In my package.json file, I have: "scripts": { "build": "build.js" } There is a build.js file in the same folder that simply console.logs. However, when I execute npm run build I receive the error messag ...

Step-by-step guide on how to prioritize rendering the login page before the ngView in AngularJS in order to

As I begin my journey with Angular JS, I am eager to implement security measures in my application. My intention is to set up a log-in page as the default landing page for my single page application. Can anyone provide guidance or recommendations on how ...

Visit a webpage on my site

Is it feasible to embed a website within another website? Suppose I have my index.html file. What if in the center of that file, we include an iFrame or similar element that loads , allowing users to explore Google directly on my website? ...

To dismiss the Div, simply click on any area outside of it. Leveraging the power of SVG, D3

I need a way to hide my div by clicking outside of it. My SVG has a background and a graph with nodes on top of that. I have a special node (circle) on the graph, clicking on which makes a box appear. To show the box, I use the following code: d3.select ...