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
check if a map inside array has multiple keys and values in Javascript

I have a following array of maps

let input = [
            {"name":"apple", "type":"fruit", "color": "red"},
            {"name":"apple", "type":"fruit", "color": "green"},
            {"name":"tomato", "type":"fruit", "color": "red", "taste":"sweet"}, 
            {"name":"tomato", "type":"fruit", "color": "green", "taste":"sour"}
            ]; 

How do I check if there are two elements in this array - one containing red apples and green tomatoes?

I tried this:

console.log (
        input.some(
          (subMap) => subMap.name == "tomato" && subMap.color == "green")
        && 
        input.some(
          (subMap) => subMap.name == "apple" && subMap.color == "red"));

For the above array, this returns true.

Looking for a more concise way of checking this.

Something like

input =[      
       {"name":"apple", "type":"fruit", "color": "red"},
       {"name":"apple", "type":"fruit", "color": "green"},
       {"name":"tomato", "type":"fruit", "color": "red", "taste":"sweet"}, 
       {"name":"tomato", "type":"fruit", "color": "green", "taste":"sour"}
       ]; 
subArray = [{"name" : "apple", "color":"red"} , {"name":"tomato", "color" : "green"}]

//This function should return true 

(input.hasElementsMatching(subArray)) => true

Thanks in advance

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

0

So, for each object in the subArray you need to find an item in the input which contains all the keys/values of that object

function arrayContainsObjects(array, check) {
  return check.every((objectToCheck) => {
    return array.some((item) => {
      return Object.entries(objectToCheck).every(
        ([key, value]) => item[key] === value
      );
    });
  });
}

const input = [{
    "name": "apple",
    "type": "fruit",
    "color": "red"
  },
  {
    "name": "apple",
    "type": "fruit",
    "color": "green"
  },
  {
    "name": "tomato",
    "type": "fruit",
    "color": "red",
    "taste": "sweet"
  },
  {
    "name": "tomato",
    "type": "fruit",
    "color": "green",
    "taste": "sour"
  }
];
const subArray = [{
  "name": "apple",
  "color": "red"
}, {
  "name": "tomato",
  "color": "green"
}];

console.log( arrayContainsObjects(input,subArray) );

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