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

139
Vistas
How to compare 2 elements inside 2 arrays? I would use the below its only for when comparing to a single value

This is not quite the function for it. How can you Loop thru each array to compare values? Or should these data structures be different? Or transformed first?

Here's the data being compared. Goal is to compare userID with DocumentID.

const videos = 
  [ { userID: '5lyU0TCyqRcTD3y7Rs2FGV8h2Sd2', name: 'Wedge Antilles',  faction: 'Rebels' } 
  , { userID: '8',                            name: 'Ciena Ree',       faction: 'Empire' } 
  , { userID: '40',                           name: 'userIDen Versio', faction: 'Empire' } 
  , { userID: '66',                           name: 'Thane Kyrell',    faction: 'Rebels' } 
  ] 
 
const blocked = 
  [ { id:  2, name: 'Wedge Antilles', documentID: '5lyU0TCyqRcTD3y7Rs2FGV8h2Sd2' } 
  , { id:  8, name: 'Ciena Ree',      documentID: 'Empire'                       } 
  , { id: 40, name: 'Iden Versio',    documentID: 'Empire'                       } 
  , { id: 66, name: 'Thane Kyrell',   documentID: 'Rebels'                       } 
  ] 
var result = videos.filter(function(videos, index) { 
  return videos["userID"] === blocked.documentID 
})

console.log(result)
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Well, you have different ways to do this. For example, you can use javascript functions map and includes in the following way:

var blockedIds = blocked.map(function(blockedItem, index) {
  return blockedItem.documentID;
});
var result = videos
  .filter(function(videos, index) {
    return blockedIds.includes(videos["userID"]);
  });

But you know that this will execute the operation in time O(nxm) (where n is the size of the first array and m is the size of the second one).

about 4 years ago · Juan Pablo Isaza Denunciar

0

I would transform these blocked into an object/map, and then try to filter through it.

const bMap = Object.fromEntries(Object.values(blocked).map(item => [item.documentID, item]))

// then
var result = videos.filter((videos) => bMap[videos.userID])
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can simply achieve it via below steps :

  • Fetch documentID using Array.map() method in a separate array as we are going to compare only documentID with the userID.
  • Now using Array.filter() method, we can compare the videos array with the documentIDArray to filtered out the matched results.

Demo :

const videos = [{
  userID: '5lyU0TCyqRcTD3y7Rs2FGV8h2Sd2',
  name: 'Wedge Antilles',
  faction: 'Rebels'
}, {
  userID: '8',
  name: 'Ciena Ree',
  faction: 'Empire'
}, {
  userID: '40',
  name: 'userIDen Versio',
  faction: 'Empire'
}, {
  userID: '66',
  name: 'Thane Kyrell',
  faction: 'Rebels'
}]; 

const blocked = [{
  id: 2,
  name: 'Wedge Antilles',
  documentID: '5lyU0TCyqRcTD3y7Rs2FGV8h2Sd2'
}, {
  id: 8,
  name: 'Ciena Ree',
  documentID: 'Empire'
}, {
  id: 40,
  name: 'Iden Versio',
  documentID: 'Empire'
}, {
  id: 66,
  name: 'Thane Kyrell',
  documentID: 'Rebels'
}];

// Fetch documentID using Array.map() method in a seperate array as we are going to compare only documentID with the userID.
const documentIDArray = blocked.map((obj) => obj.documentID);

// Now using Array.filter() method, we can compare the videos array with the documentIDArray to filtered out the matched results.
var result = videos.filter(video => documentIDArray.includes(video['userID']));

console.log(result);

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