1

I have a react component like

class MyApp extends React.Component{
  constructor(props) {
    super(props);
     this.state = {
      data =[
        {key=0,name="abc",value="123"},
        {key=1,name="def",value="456"},
        {key=2,name="ghi",value="789"}
      ]
    }
  }

  renameArrayKeys = (key) => {
    this.setState(oldState => {
      return {
        data: oldState.data.map((item, index) => item.key= index + 1)
      }
    })
  }
  render() {     
    return(
      <button onClick={this.renameArrayKeys}> Click Me </button>
    )
  }
}

What I want is finally my data in state should look like:

data =[
    {key=1,name="abc",value="123"},
    {key=2,name="def",value="456"},
    {key=3,name="ghi",value="789"}
]

Can somebody review my code and tell me what is wrong here as I am not able to get desired data. The value of the key in my array is not changing.

2
  • Inside Constructor, this.state: { data =[ {key:0,name:"abc",value:"123"}, {key:1,name:"def",value:"456"}, {key:2,name:"ghi",value:"789"} ] } Use : instead of = Commented Jan 21, 2018 at 9:44
  • ill correct it. Its a typo but the problem remains same Commented Jan 21, 2018 at 9:48

4 Answers 4

2

Array#map should return an object, and not the results of the key assignment. You can use Object#assign to add the key, and return a new object.

    class MyApp extends React.Component {
      constructor(props) {
        super(props)

        this.state = {
          data: [
            {key: 0,name="abc",value="123"},
            {key: 1,name="def",value="456"},
            {key: 2,name="ghi",value="789"}
          ]
        }
      }

      renameArrayKeys = (key) => {
        this.setState(oldState => {
          return {
            // create a new item object with the new key
            data: oldState.data.map((item, index) => Object.assign({}, item,    {
              key: index + 1
            }))
          }
        })
      }
      render() {
        return (
            <button onClick={this.renameArrayKeys}> Click Me </button>
        )
      }
    }
Sign up to request clarification or add additional context in comments.

Comments

2

The mapping function returns a new object after modifying it, thus assigning a value in the mapping function does not make sense (It retuns the assignment value and not the modified object). This is more of a javascript question then react, since the only thing that is wrong is the mapping function.

I've modified this part and added some insights.

data =[
        {key:0,name:"abc",value:"123"},
        {key:1,name:"def",value:"456"},
        {key:2,name:"ghi",value:"789"}
        ]
       
// This all evaluates to just the numbers since you are turning all the items into a javascript initialization command, which returns its value.
const yourOutput = data.map((item, index) => item.key = index + 1) 


// What you should do is make the mapping change just the specific key that you want
// There are many ways to do it, this is my way.
const output =  data.map((item,index)=>({...item,key:index+1}))

console.log(yourOutput)
console.log(output)

Comments

1

Easy Way

let updateData = [];
  this.state.data.map((item, index) => {
    updateData.push({
      key: item.key+1, name: item.name, value: item.value,
    });
  });

 this.setState({ data: updatedData });

Comments

0

It seems your map(...) structure is wrong!

map function isn't a place for change something using '='. this just will return an array and you should assign whole objects to it.

try this:

class MyApp extends React.Component{
  constructor(props) {
    super(props);
     this.state = {
      data =[
        {key=0,name="abc",value="123"},
        {key=1,name="def",value="456"},
        {key=2,name="ghi",value="789"}
      ]
    }
    this.renameArrayKeys = this.renameArrayKeys.bind(this)
  }

  renameArrayKeys(key){
    this.setState(oldState => {
      oldState.data = oldState.data.map((item, index) => 
        { key: index+1, name = item.name, value = item.value }
      return oldState
    })
  }
  render() {     
    return(
      <button onClick={this.renameArrayKeys}> Click Me </button>
    )
  }
}

and you can use es6 style for object and array assign like this :

oldState.data.map((item, index) => { ...item, key: index+1}

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.