Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
¿Cómo agregar un nuevo valor clave para reaccionar js state array?

¿Quiero agregar un nuevo valor clave a la matriz en el estado? Te digo lo que quiero hacer a continuación. ¿Cómo puedo hacer eso? 1. Estado de inicio en el método de constitución

 `this.state = { files: [] }'
  1. Después de configurar el estado con archivos activos

    this.setState({archivos: archivos activos})

  2. Pantalla de mi estado

    { files: [ { key1: val1, key2: val2, key3: val3 }, { key1: val1, key2: val2, key3: val3 }, { key1: val1, key2: val2, key3: val3 } ] }

  3. ¿Cómo agregar un nuevo valor clave para cada archivo? El estado que quiero

    { files: [ { key1: val1, key2: val2, key3: val3, key4: val4 }, { key1: val1, key2: val2, key3: val3, key4: val4 }, { key1: val1, key2: val2, key3: val3, key4: val4 } ] }

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Creo que esto cumplirá con el escenario anterior.

 const newFile = this.state.files.map((file) => { return {...file, key4: val4}; }); this.setState({files: newFile });
over 4 years ago · Santiago Trujillo Report

0

Puede hacer uso de forEach y agregar el nuevo valor como

 var newState = [...this.state.files]; newState.forEach(function(file) { file.key4 = "val4" }) this.setState({files: newState}, function() { console.log(this.state.files); })

Violín

 class App extends React.Component { constructor() { super(); this.state = { files: [ { key1: "val1", key2: "val2", key3: "val3" }, { key1: "val1", key2: "val2", key3: "val3" }, { key1: "val1", key2: "val2", key3: "val3" } ] } } componentDidMount() { console.log(this.state.files) ; var newState = [...this.state.files]; newState.forEach(function(file) { file.key4 = "val4" }) this.setState({files: newState}, function() { console.log(this.state.files); }) } render() { return <div>Hello</div> } } ReactDOM.render(<App/>, document.getElementById('app'));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="app"></div>

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!