1

i want to replace the values of the nested array object like the below one, when button is clicked it will replace the old values of the x indexed object and set the new values there.

class compo extends React.Component {
    constructor() {
        super();
        this.state = {
            tabsData:[
               {
                  id:1,
                  title:"OldTitle1"
               },
               {
                  id:2,
                  title:"OldTitle2"
               }
            ],
        }
        this.changeTab = this.changeTab.bind(this)
    }
    changeTab(){
       const newData={
           id=3,
           title="New One"
       }
       //replace the above new data in the second object of nested array in state
    }
    render(){
        return(
            <button type="button" >Add</button>
        )
    ;}
}
export default compo

the state should be like this after

tabsData:[
      {
         id:1,
         title:"OldTitle"
      },
      {
         id:3,
         title:"New One"
      }
  ]
3
  • you want to replace the last index of tabs data.to do this, then pop element using arr. pop().it will pop the last element of the array and then push new data Commented Mar 30, 2021 at 11:12
  • Take a look at stackoverflow.com/questions/29537299/… Commented Mar 30, 2021 at 11:14
  • not the exactly last one it could be anywhere in the array Commented Mar 31, 2021 at 10:03

2 Answers 2

1

Not able to comment as my rep is less than 50...based on an idea of what you need here is the code.

https://codesandbox.io/s/brave-lumiere-dh9ry?file=/src/App.js

const [data, setData] = React.useState([
    {
      id: 1,
      title: "OldTitle1"
    },
    {
      id: 2,
      title: "OldTitle2"
    }
  ]);
  const newData = { id: 3, title: "New One" };

  const addData = () => {
    const newArr = data;
    newArr[1] = newData;
    console.log("newArr>>>>", newArr);
    setData([...newArr]);
  };
Sign up to request clarification or add additional context in comments.

Comments

1

You could do something like this...

import React from "react";

class compo extends React.Component {
  constructor() {
    super();
    this.state = {
      tabsData: [
        {
          id: 1,
          title: "OldTitle1"
        },
        {
          id: 2,
          title: "OldTitle2"
        }
      ]
    };
    this.changeTab = this.changeTab.bind(this);
  }
  changeTab() {
    const newData = {
      id: 3,
      title: "New One"
    };
    // Make duplicate since you can't mutatue state
    let newTabData = [...this.state.tabsData];

    const id = 2; // id to be removed

    // CASE 1: If you want to maintain order
    const index = newTabData.findIndex((data) => data.id === id);
    if (index > -1) {
      // replace oldData with newData
      newTabData.splice(index, 1, newData);
    } else {
      // simply add newData at last
      newTabData.push(newData);
    }

    // CASE 2: If order doesn't matter
    // // remove oldData
    // newTabData = newTabData.filter((data) => data.id !== id);
    // // add new data at last
    // newTabData.push(newData);


    // finally update the state irrespective of any case
    this.setState({ tabsData: newTabData });
  }
  render() {
    return (
      <div>
        <button type="button">
          Add
        </button>
        <button type="button" onClick={this.changeTab}>
          Change
        </button>
        <br />
        {JSON.stringify(this.state, null, 2)}
      </div>
    );
  }
}
export default compo;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.