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

240
Views
¿Cómo encontrar los elementos comunes en 2 matrices en Javascript sin duplicados (a menos que haya 2 del mismo elemento)?

Por ejemplo, si tengo 2 matrices siendo

 ['dog', 'cat', 'zebra', 'lion', 'cat']

y

 ['dog', 'cat', 'cat', 'frog']

entonces se debe crear una nueva matriz que solo contenga

 ['dog', 'cat', 'cat']
almost 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Puede usar dos bucles para el mismo y compararlos entre sí. Ej.

 for(let i=0; i<ar1.length; i++)
 {
 for(let j=0; j<ar2.length; j++){
 if(ar1[i]==ar[2])
 res.push(ar[i]);
almost 4 years ago · Santiago Trujillo Report

0

 var array1 = ['dog', 'cat', 'zebra', 'lion', 'cat'];
var array2 = ['dog', 'cat', 'cat', 'frog'];

// Then you can use filter

// const filteredArray = array1.filter(value => array2.includes(value));


// (OR) `indexOf` for old browsers compability

var filteredArray = array1.filter(function(n) {
 return array2.indexOf(n) !== -1;
});

console.log(filteredArray);

almost 4 years ago · Santiago Trujillo Report

0

 const a = ['dog', 'cat', 'zebra', 'lion', 'cat'];
const b = ['dog', 'cat', 'cat', 'frog'];

let result = a.filter(function(obj){
 return b.indexOf(obj) !== -1
})
 
console.log(result)

almost 4 years ago · Santiago Trujillo 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!