Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

139
Vistas
How select items in an object from an array of undesired key and create a new object with the result

Maybe it's a weird question, and maybe that exists and other way to make that... But, I've an initial Object on which I iterate on the key. For a special task, I've needed to have this Object without some key:value. For doing that I make an object with the key that I don't want.

I make this code pen for sample: 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'
// }

I've found this solution but I can't find it very readable:

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 Respuestas
Responde la pregunta

0

You could do this,

Use Array.prototype.map() on Object.entires() to filter out an array of desired key-value pair. And finally construct a new object from the key-value pair using 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 Denunciar

0

You could filter the objects entries by keeping the ones that evaluate falsy to find(x => k.includes(x)) and use them to create a new 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 result = Object.fromEntries(Object.entries(myInitialObject)
  .filter(([k]) => !unDesiredKey.find(x => k.includes(x)))
);

console.log(result);

I could also accomplish this with reduce() if you like.

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 Denunciar

0

You could filter the entries and slice the key for comparing.

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda