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

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

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 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!