0

I have the following code, which basically adds empty objects to an array.

  handleAddNewRow = () => {
    this.setState({
      rowData: [
        { MEMBER: "", ALIAS: "", STATUS: "" },
        ...this.state.rowData
      ]
    })
}

Lets say, I am passing an integer value to the function handleAddNewRow and then it dynamically adds the number of empty objects to the array based on the integer value, How is it possible?

2 Answers 2

2

You can look at my function:

handleAddNewRow = (number) => {
   this.setState({
       rowData: [
            ...this.state.rowData,
            ...(new Array(number).fill({ MEMBER: "", ALIAS: "", STATUS: "" }))
       ]
   });
}
Sign up to request clarification or add additional context in comments.

2 Comments

it does not work. It only adds 1 empty object.
I've tested on devtool and it works as well. Can you give me a code so I can test it?
0

in the following code i wrote code in simple condition change it on your own

const array = [{name: '', family: ''}]
function a(num, arr) {
  let temp = [...arr, {name: '', family: ''}]
  if (num - 1 > 0) {
    temp = a(num - 1, temp)
  }
return temp
}
const b = a(4, array)
console.log(b)

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.