Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

67
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!