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

109
Visualizações
Leave objects by the list of keys. Pure JS

I want to create a new array of objects without the gender field. Where is the mistake?

let collections = [
    { name: "Tom", gender: "male", age: 12 },
    { name: "Becky", gender: "female", age: 11 },
    { name: "Huck", gender: "male", age: 13 }
  ];
  let newCollections = collections.slice();
  let operations = {
    select: function () {
      let args = [].slice.call(arguments);
      for (let i = 0; i < newCollections.length; i++) {
        for (let key in newCollections[i]) {
          for (let j = 0; j < args.length; j++) {
            if (key !== args[j]) {
              delete key;
              return newCollections;
            } } } } } }; 
const result = operations.select("name", "age");// the list of fields to save

console.log(result);

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can do it using map method:

const array_without_gender = collections.map(el => {
    return {name: el.name, age: el.age};
})

This code will create new array filled by objects without gender key,

about 4 years ago · Juan Pablo Isaza Relatório

0

You could create an omit() function that will return an object without a given property.

We can then use Array.map() to run each item in the array through this:

let collections = [ { name: "Tom", gender: "male", age: 12 }, { name: "Becky", gender: "female", age: 11 }, { name: "Huck", gender: "male", age: 13 } ];

function omit(obj, field) {
    return (({ [field]: _, ...result }) => result)(obj);
}

let newCollections = collections.map(person => omit(person, 'gender'));
console.log('New collections:', newCollections);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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