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

136
Views
Cómo seleccionar elementos en un objeto de una matriz de claves no deseadas y crear un nuevo objeto con el resultado

Tal vez sea una pregunta extraña, y tal vez eso exista y otra forma de hacer eso ... Pero, tengo un Objeto inicial en el que itero en la clave. Para una tarea especial, necesitaba tener este Objeto sin alguna clave: valor. Para hacer eso hago un objeto con la llave que no quiero.

Hago este lápiz de código para muestra: https://codepen.io/charlene-bx/pen/qBXaqEL

 const myInitialObject = { 'key#one' : 'dataofanotherObject', 'key#two' : 'dataofanotherObject', 'key#three' : 'dataofanotherObject', 'key#four' : 'dataofanotherObject', 'key#five' : 'dataofanotherObject', 'key#six' : 'dataofanotherObject', 'key#seven' : 'dataofanotherObject', 'key#eight' : 'dataofanotherObject', 'key#nine' : 'dataofanotherObject', 'key#ten' : 'dataofanotherObject' } const unDesiredKey = [ 'one', 'four', 'six' ] // expected output: // { // 'key#two' : 'dataofanotherObject', // 'key#three' : 'dataofanotherObject', // 'key#five' : 'dataofanotherObject', // 'key#seven' : 'dataofanotherObject', // 'key#eight' : 'dataofanotherObject', // 'key#nine' : 'dataofanotherObject', // 'key#ten' : 'dataofanotherObject' // }

Encontré esta solución pero no puedo encontrarla muy legible:

 const filteredObject = Object.keys(myInitialObject) .filter(key => !unDesiredKey.map( el => !key.includes(el)).some(el => el===false)) .reduce((accumulator, value) => ({...accumulator, [value]: myInitialObject[value]}), {})
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Podrías hacer esto,

Use Array.prototype.map() en Object.entires() para filtrar una matriz del par clave-valor deseado. Y finalmente construya un nuevo objeto a partir del par clave-valor usando Object.fromEntries()

 const myInitialObject = { 'key#one': 'dataofanotherObject', 'key#two': 'dataofanotherObject', 'key#three': 'dataofanotherObject', 'key#four': 'dataofanotherObject', 'key#five': 'dataofanotherObject', 'key#six': 'dataofanotherObject', 'key#seven': 'dataofanotherObject', 'key#eight': 'dataofanotherObject', 'key#nine': 'dataofanotherObject', 'key#ten': 'dataofanotherObject' } const unDesiredKey = [ 'one', 'four', 'six' ] const arr = Object.entries(myInitialObject).filter(x => !unDesiredKey.includes(x[0].split('#')[1])); const finalObj = Object.fromEntries(arr); console.log(finalObj)

about 4 years ago · Juan Pablo Isaza Report

0

Puede filter las entries de los objetos manteniendo las que evalúan falsy para find(x => k.includes(x)) y usarlas para crear un nuevo objeto fromEntries .

 const myInitialObject = { 'key#one': 'dataofanotherObject', 'key#two': 'dataofanotherObject', 'key#three': 'dataofanotherObject', 'key#four': 'dataofanotherObject', 'key#five': 'dataofanotherObject', 'key#six': 'dataofanotherObject', 'key#seven': 'dataofanotherObject', 'key#eight': 'dataofanotherObject', 'key#nine': 'dataofanotherObject', 'key#ten': 'dataofanotherObject' } const unDesiredKey = [ 'one', 'four', 'six' ]; const result = Object.fromEntries(Object.entries(myInitialObject) .filter(([k]) => !unDesiredKey.find(x => k.includes(x))) ); console.log(result);

También podría lograr esto con reduce() si lo desea.

 const obj = { 'key#one': 'dataofanotherObject', 'key#two': 'dataofanotherObject', 'key#three': 'dataofanotherObject', 'key#four': 'dataofanotherObject', 'key#five': 'dataofanotherObject', 'key#six': 'dataofanotherObject', 'key#seven': 'dataofanotherObject', 'key#eight': 'dataofanotherObject', 'key#nine': 'dataofanotherObject', 'key#ten': 'dataofanotherObject' } const unDesiredKey = [ 'one', 'four', 'six' ]; const result = Object.entries(obj).reduce((acc, [k, v]) => { if (unDesiredKey.find(x => k.includes(x))) return acc; acc = { ...acc, [k]: v } return acc; }, {}); console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Puede filtrar las entradas y dividir la clave para comparar.

 const data = { 'key#one': 'dataofanotherObject', 'key#two': 'dataofanotherObject', 'key#three': 'dataofanotherObject', 'key#four': 'dataofanotherObject', 'key#five': 'dataofanotherObject', 'key#six': 'dataofanotherObject', 'key#seven': 'dataofanotherObject', 'key#eight': 'dataofanotherObject', 'key#nine': 'dataofanotherObject', 'key#ten': 'dataofanotherObject' }, unDesiredKey = ['one', 'four', 'six'], result = Object.fromEntries(Object .entries(data) .filter(([key]) => !unDesiredKey.includes(key.slice(key.indexOf('#') + 1))) ); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

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!