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

131
Views
How to display all objects that contain all item from array - javascript

Need for some help here. Looking for a hint to solve this issue :

The goal is to filter arrayOfObject and get all objects with the property fruits containing all the element from the given array.

  const arrayOfObject = [
  {
    id: 1,
    country: 'USA',
    fruits: ["APPLE", "ORANGE", "BANANA"]
  },
  {
    id: 2,
    country: 'Canada',
    fruits: ["APPLE"]
  },
  {
    id: 3,
    country: 'France',
    fruits: ["ORANGE", "BANANA", "LEMON"]
  },
  {
    id: 4,
    country: 'Mexico',
    fruits: ["BANANA", "PYTHON", "CHERRY"]
  },
  {
    id: 5,
    country: 'Ukraine',
    fruits: ["APPLE", "ORANGE", "CHERRY"]
  },
  {
    id: 6,
    country: 'Italy',
    fruits: ["APPLE", "ORANGE", "BANANA", "LEMON", "CHERRY"]
  }
];

First exemple with this given array :

const firstArrayOfFruits = ["APPLE","ORANGE","BANANA"];

Should render =>

[
  {
    id: 1,
    country: 'USA',
    fruits: ["APPLE", "ORANGE", "BANANA"]
  },
  {
    id: 6,
    country: 'Italy',
    fruits: ["APPLE", "ORANGE", "BANANA", "LEMON", "CHERRY"]
  }
]

Second exemple with this given array :

const secondArrayOfFruits = ["APPLE","ORANGE"];

Should render =>

[
  {
    id: 1,
    country: 'USA',
    fruits: ["APPLE", "ORANGE", "BANANA"]
  },
  {
    id: 5,
    country: 'Ukraine',
    fruits: ["APPLE", "ORANGE", "CHERRY"]
  },
  {
    id: 6,
    country: 'Italy',
    fruits: ["APPLE", "ORANGE", "BANANA", "LEMON", "CHERRY"]
  }
]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use Array.prototype.filter:

const arrayOfObject = [{
    id: 1,
    country: 'USA',
    fruits: ["APPLE", "ORANGE", "BANANA"]
  },
  {
    id: 2,
    country: 'Canada',
    fruits: ["APPLE"]
  },
  {
    id: 3,
    country: 'France',
    fruits: ["ORANGE", "BANANA", "LEMON"]
  },
  {
    id: 4,
    country: 'Mexico',
    fruits: ["BANANA", "PYTHON", "CHERRY"]
  },
  {
    id: 5,
    country: 'Ukraine',
    fruits: ["APPLE", "ORANGE", "CHERRY"]
  },
  {
    id: 6,
    country: 'Italy',
    fruits: ["APPLE", "ORANGE", "BANANA", "LEMON", "CHERRY"]
  }
];

const firstArrayOfFruits = ["APPLE", "ORANGE", "BANANA"];

var arr = arrayOfObject.filter(item => item.fruits.filter(fruit => firstArrayOfFruits.indexOf(fruit) + 1).length >= firstArrayOfFruits.length);
console.log(arr);

about 4 years ago · Juan Pablo Isaza Report

0

Using ES6 some() function is useful because when finds an entry it will return true and doesnt check the rest, so you can save some calcs, time and CPU

const filtered = arrayOfObject.filter(obj => {
       if(obj.fruits.some(fruit => fruitsLisToCheck.includes(fruit)))
         return obj
    })

const fruitsLisToCheck = ['BANANA', 'APPLE'];

const arrayOfObject = [
  {
    id: 2,
    country: 'Canada',
    fruits: ["APPLE"]
  },
  {
    id: 3,
    country: 'France',
    fruits: ["BANANA"]
  },
  {
    id: 4,
    country: 'Mexico',
    fruits: ["CHERRY"]
  },
];
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!