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

173
Views
Cómo usar la clave de objeto del valor de cadena en javascript

Tengo un objeto de muestra, necesito reasignar el valor dinámicamente.

aquí hay un objeto de muestra, he almacenado el objeto en dynamodb con el tipo de datos inarray. He formado el objeto usando loop. No pude usar el valor payload.offer. amablemente sugiera la mejor solución.

 { organization_id: 4002400004, organization_name: 'Velocity Global Integration Sandbox', 'payload.offer': 'payload offer', }

Formato de salida requerido

 { organization_id: 4002400004, organization_name: 'Velocity Global Integration Sandbox', 'payload': { offer: 'payload offer' }, }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Según tengo entendido, desea una forma de convertir de notación de puntos a objetos profundamente anidados. Puedes probar lo siguiente entonces:

 function dotToNested(obj) { return Object.entries(obj).reduce((newObj, [key, value]) => { const path = key.split("."); // This part makes it recursive, you can just use value if you don't want that const newValue = typeof value === "object" && !!value ? dotToNested(value) : value; if (path.length > 1) { // Last key will be used for assignment const newKey = path.pop(); // Get the object to assign to, and create the intermediate objects along the way const objRef = path.reduce((ref, key) => { // create the intermediate object ref[key] = {}; // move further down in the deeply nested structure return ref[key]; }, newObj); objRef[newKey] = newValue; } else { // Just a "regular" key, assign directly to it, no need for the path newObj[key] = newValue; } return newObj; }, {}); } const dotNotation = { organization_id: 4002400004, organization_name: 'Velocity Global Integration Sandbox', 'payload.offer': 'payload offer', }; const deeplyNested = dotToNested(dotNotation); console.log({ dotNotation, deeplyNested });

about 4 years ago · Juan Pablo Isaza Report

0

Puede usar el método de nivel superior Object.keys() del prototipo de objeto estándar y dividir ('.') la clave de destino e intentar eliminar la clave anterior y agregar la clave nueva.

Bueno, esta es mi sugerencia de código.

 const input = { organization_id: 4002400004, organization_name: 'Velocity Global Integration Sandbox', 'payload.offer': 'payload offer', }; function modify(input, requireKey) { const newObject = {}; Object.keys(input).forEach((key) => { if (key === requireKey) { let [newKey, newDownLevelKey] = key.split('.'); delete Object.assign(newObject, input, { [newKey]: input[key], })[key]; newObject[newKey] = { [newDownLevelKey]: newObject[newKey], }; } }); return newObject; } console.log(modify(input, 'payload.offer'));

Recursos :

Objeto.assign(): https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

Objeto.keys() : https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

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!