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

201
Visualizações
How to check if any item in object matches any key in other array

I want to see if any item in arr2 matches any top level key from arr1 (forms or assets).

First array:

const arr1 = {
  forms: {
    permissions: {
      create: true,
      edit: true,
      delete: true,
    },
  },
  assets: {
    permissions: {
      create: true,
      edit: true,
      delete: true,
    },
  },
};

Second array:

const arr2 = ["assets", "media"];

I have tried this:

if (arr2 in arr1) {
    console.log("key is in contained in object");
}

But this only works when there is only one item in arr2.

almost 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

For the array use Array#some with a test using Object#hasOwnProperty.

For objects, you don't need to enumerate the keys of the object (O(n)): you can perform a direct test for the presence of a key (O(1)).

const o = {
  forms: {
    permissions: {
      create: true,
      edit: true,
      delete: true,
    },
  },
  assets: {
    permissions: {
      create: true,
      edit: true,
      delete: true,
    },
  },
}

const arr = ["assets", "media"]

console.log(arr.some((k) => o.hasOwnProperty(k))) // true

almost 4 years ago · Santiago Trujillo Relatório

0

You can get an array of the object's top level keys and then check if any of them are included in the other array.

Like so:

const arr1 = {
  forms: {
    permissions: {
      create: true,
      edit: true,
      delete: true,
    },
  },
  assets: {
    permissions: {
      create: true,
      edit: true,
      delete: true,
    },
  },
};

const arr2 = ["assets", "media"];

function hasSomeTopLevelKey(object, arrayOfKeys) {
   return Object.keys(object)
   .some(topLevelKey => arrayOfKeys.includes(topLevelKey));
}

const x = hasSomeTopLevelKey(arr1, arr2)
console.log(x)

almost 4 years ago · Santiago Trujillo 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