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

256
Vistas
Remove entries from an object where the keys aren't in an array

I'm trying to remove entries from an object so that it only includes those that are in an array. So I have a required array:

const required = ['accountNumber', 'packRequested', 'collectionDate']

And a touchedFields object:

const touchedFields = {
    accountNumber: true,
    sortCode: false,
    account: true,
    collectionDate: false,
    packRequested: false
}

This is what I am after:

const touchedFields = {
        accountNumber: true,
        collectionDate: false,
        packRequested: false
    }

How can this be achieved?

I've tried instead creating an array output (as I couldn't do the object), looping through each object entry, and the looping through each array key, comparing values and pushing to a new array if the values matches:

let newArr = []
for (let [key, value] of Object.entries(requiredFields)) {
    Object.keys(touchedFields).forEach((cur) => {
        if (cur == value) {
            newArr.push({cur: value})
        }
    })
}
console.log(newArr)

But even that doesn't set the values properly as it set's the key as cur and not the actual cur variable:

[{cur: 'accountNumber'}, {cur: 'packRequested'}, {cur: 'collectionDate'}]

Any help would be greatly appreciated.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0


const required = ['accountNumber', 'packRequested', 'collectionDate']

const touchedFields = {
    accountNumber: true,
    sortCode: false,
    account: true,
    collectionDate: false,
    packRequested: false
}

let result = Object.fromEntries(
  Object.entries(touchedFields)
  .filter(([k,v]) => required.includes(k))
)

console.log(result)
about 4 years ago · Juan Pablo Isaza Denunciar

0

Use Array.reduce method

const required = ['accountNumber', 'packRequested', 'collectionDate']

const touchedFields = {
  accountNumber: true,
  sortCode: false,
  account: true,
  collectionDate: false,
  packRequested: false
}

const result = required.reduce((acc, key) => {
  acc[key] = touchedFields[key];
  return acc;
}, {});

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you want to alter the orginal object, you can loop over the keys and delete the ones that are not in the required array.

const required = ['accountNumber', 'packRequested', 'collectionDate']

const touchedFields = {
  accountNumber: true,
  sortCode: false,
  account: true,
  collectionDate: false,
  packRequested: false
}

const pruneObject = (obj, keys) => Object.keys(obj).forEach(
  key => !required.includes(key) && delete obj[key]
);

pruneObject(touchedFields, required);
console.log(touchedFields);

If you do not want to alter the original object you can loop over the array and build a new object from it.

const required = ['accountNumber', 'packRequested', 'collectionDate']

const touchedFields = {
  accountNumber: true,
  sortCode: false,
  account: true,
  collectionDate: false,
  packRequested: false
}

const inclusivePick = (obj, keys) => Object.fromEntries(
  keys.map(key => [key, obj[key]])
);


const result = inclusivePick(touchedFields, required);
console.log(result);

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