Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

149
Visualizações
How to create a function that omits key/value pairs from an object, and creates a new object with the remaining properties and values?

I've been working on creating an omit function with a source and keys parameter, that checks for keys within the source, and if they are found, those properties are omitted from the source, then create a new object literal with the remaining key/value pairs within the source. So far, I've been unsuccessful, and only have been able to create a function that does the opposite of creating an object with the keys found within the source. Can anyone help figure out what I'm missing? It would be very appreciated!

function omit(source, keys) {
  var includedSource = {};
  for (var key in source) {
    for (var i = 0; i < keys.length; i++) {
      if (keys[i] !== key) {
        includedSource[key] = source[key];
        console.log('source[keys[i]]:', source[keys[i]]);
        console.log('includedSource:', includedSource);
      }
    }
  }
  return includedSource;
}

  function omit(source, keys) {
    var includedSource = {};
    for (var key in source) {
      for (var i = 0; i < keys.length; i++) {
        if (keys[i] === key) {
          includedSource[key] = source[key];
          console.log('source[keys[i]]:', source[keys[i]]);
          console.log('includedSource:', includedSource);
          }
        }
     }
     return includedSource;
   }
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Here is a simple implementation with flat object. Im not sure how would keys be declared if your object might be nested, and you would expect to extract some of N level of nested value/keys

const obj = {
  A: 1,
  B: 2,
  C: 3,
  D: 4
};

function omit(data, keys) {
  let result = {};

  for (let key in data) {
    if (keys.includes(key)) continue;

    result[key] = data[key];
  }

  return result;
}

console.log(omit(obj, ["A", "D"])) // {B: 2, C: 3}
about 4 years ago · Juan Pablo Isaza Relatório

0

You could destructure and rest for a new object.

function omit(object, keys) {
    let _; // prevent to be global
    for (const key of keys) ({ [key]: _, ...object } = object);
    return object;
}

console.log(omit({ a: 1, b: 2, c: 3, d: 4 }, ['b', 'c']));

about 4 years ago · Juan Pablo Isaza Relatório

0

One approach is use Object.entries() and filter the entries out that have the unwanted keys then use Object.fromEntries() to create the new object from the filtered entries

const omit = (data, keys) => {
  return Object.fromEntries(
    Object.entries(data).filter(([k]) => !keys.includes(k))
  )
}


console.log(omit({ a:1,b:2,c:3,d:4}, ['a', 'b']))

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda