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

58
Views
¿Cómo restablecer el valor de un objeto anidado?

Debajo del objeto dado, necesito restablecer el valor de la clave "etiqueta", "valor", "sala" y "tole" para vaciar (' ') usando javascript.

 state ={ "label": "लुम्बिनी प्रदेश", "value": 21, "province": { "label": "लुम्बिनी प्रदेश", "value": "23" }, "district": { "label": "अर्घाखाँची", "value": "20" }, "local_level": { "label": "भूमिकास्थान नगरपालिका", "value": "17" }, "ward": "10", "tole": "venas" }

He intentado obtener las claves del objeto.

 Object.keys(state).map(item=>({key: item}))

pero no tengo idea de cómo restablecer el valor del objeto.

El resultado final debería verse así. el objeto de estado puede evolucionar, es decir, la clave y el valor del objeto pueden agregarse o eliminarse.

 state ={ "label": '', "value": '', "province": { "label": '', "value": '' }, "district": { "label": '', "value": '' }, "local_level": { "label": '', "value": '' }, "ward": '', "tole": '' }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puede iterar a través del objeto usando for...in Loop en JS. Y luego, si encuentra esas claves, simplemente reemplace sus valores con una cadena vacía. Y dado que también desea verificar nuevamente que si el valor de las claves son objetos, y si lo son, puede volver a recorrerlos y cambiar su valor a una cadena vacía.

 for (let key in state) { if (typeof state[key] === 'object') { // if the value of the key is an object, then we will again loop through the value of that key, and change the value of those keys to an empty string. for (let nestedKey in state[key]) { // console.log(nestedKey, state[key][nestedKey]); if ( nestedKey === "label" || nestedKey === "value" || nestedKey === "ward" || nestedKey === "tole" ) { state[key][nestedKey] = " "; } } } if (key === "label" || key === "value" || key === "ward" || key === "tole") { state[key] = " "; } }

Este código funcionará y cambiará todos esos valores a una cadena vacía.

about 4 years ago · Juan Pablo Isaza Report

0

let setToDefault = state => Object.assign(state, { label: '', value: '', ward: '', tole: '' });
about 4 years ago · Juan Pablo Isaza 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!