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

68
Visualizações
Check array of strings against array of objects and return something per each case

I have a simple array problem: I have two different arrays: one with strings and one with objects. I need to check one agains the other in a certain way: Array of objects needs to check if a property of the object is included in array of strings, and return a response in each case.

const colors = ["blue", "pink", "red", "green", "yellow", "orange", "white"]

const objColors = [{name:"pink", value: true}, {name:"green", value: true}, {name: "white", value: false}] 

My expected response array would be something like:

const res = [false, true, false, true, false, false, false]

I don't know how to tackle this, as I've tried several things with no success. I tried double iterations, but it gave me a wrong response. I've also tried the method includes, but then I can only check my objColors array, therefore I don't get a response for all the cases I need to check

let res = objects.map(x => (strings.includes(x.name)))

Could someone please give me a hint on how to check them to get the desired response? Thanks in advance

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

0

First turn the array of objects into a structure that's easier to check through - perhaps a Map, or a Set of the values that are true. Then you can map the colors array and look up the associated value.

const colors = ["blue", "pink", "red", "green", "yellow", "orange", "white"];
const objColors = [{name:"pink", value: true}, {name:"green", value: true}, {name: "white", value: false}];


const trueColors = new Set(
  objColors
    .filter(({ value }) => value)
    .map(({ name }) => name)
);
const res = colors.map(color => trueColors.has(color));
console.log(res);

about 4 years ago · Juan Pablo Isaza Relatório

0

An approach with an object.

const
    colors = ["blue", "pink", "red", "green", "yellow", "orange", "white"],
    objColors = [{ name: "pink", value: true }, { name: "green", value: true }, { name: "white", value: false }],
    wanted = Object.fromEntries(objColors.map(({ name, value }) => [name, value])),
    result = colors.map(c => !!wanted[c]);

console.log(result);

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