Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

149
Vistas
Javascript; pushing common elements from 2 lists to a new list
let listOne = [Bill, Joe, Trever, Neil, Jim, Pam, Michael]
let listTwo = [Petter, Pam, Steven, Jim, Michael, Scott]

I have two lists but I want to create a new list with only the names in both lists.

[Jim, Pam, Michael]

What I did:

I create a nested loop to push each name into a new list

what I need help with:

I feel there is a better way to do this. without having to nest my loops. Perhaps somehow filtering through both lists simultaneously somehow

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can make use of Set and filter here. Checking the existence of name in Set is efficient, if you gonna use has method of set.

let listOne = ["Bill", "Joe", "Trever", "Neil", "Jim", "Pam", "Michael"];
let listTwo = ["Petter", "Pam", "Steven", "Jim", "Michael", "Scott"];

const set = new Set(listOne);
const result = listTwo.filter(name => set.has(name));
// or
// const result = listTwo.filter(set.has.bind(set));
console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could use Array.prototype.reduce, have the callback search the other list, and only insert the name if it is in the other list.

listOne.reduce((prev, cur)=>{
    if(listTwo.includes(cur)){
        prev.push(cur);
    }
    return prev;
});

EDIT: After further testing, Array.prototype.reduce requires the first and second arguments to have a type of number, so I would recommend using filter instead. (see other answers)
However, if you want to use reduce, you would have to use something like this:

let listOne = ["Bill", "Joe", "Trever", "Neil", "Jim", "Pam", "Michael"];
let listTwo = ["Petter", "Pam", "Steven", "Jim", "Michael", "Scott"];
let results = [];
listOne.reduce((prev, cur)=> {
  if(listTwo.includes(cur)){
    results.push(cur);
  }
  return;
});
console.log(results); // Prints [ 'Jim', 'Pam', 'Michael' ]

My recommendation is to use the accepted answer by Vitor Franca and use Array.prototype.filter

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can do something like this:

let listOne = [Bill, Joe, Trever, Neil, Jim, Pam, Michael]
let listTwo = [Petter, Pam, Steven, Jim, Michael, Scott]

let result = listOne.filter(item => listTwo.includes(item))
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda