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

238
Vistas
Split array of objects into 2 arrays of matching and non-matching items
const users = 
[
  {
    _id: "5fbfa729fc46a415ce5503a6",
    first_name: 'Allen',
    last_name: 'Border',
    timestamp: 1606395689121,
    key: [ "5fbf6f91aff7f3320a906547", "5fbfa748fc46a415ce5503a8" ]
  },
  {
    _id: "5fbfa6fbfc46a415ce5503a4",
    first_name: 'james',
    last_name: 'roger',
    timestamp: 1606395689125
  },
  {
    _id: "5fbf6f91aff7f3320a906547",
    first_name: 'david',
    last_name: 'gosh',
    timestamp: 1606395689130,
    key: [ "5fbfa729fc46a415ce5503a6" ]
  },
  {
    _id: "5e4e74eb380054797d9db623",
    first_name: 'Ricky',
    last_name: 'bichel',
    timestamp: 1606395689131
  }
]

const user_id = "5fbfa748fc46a415ce5503a8";

how to seperate match not match data if user_id matched with users.key it should have matched data that does not match should have notMatch data. can we separate two results match and not match records ?

Does not work

const result = users.key.indexOf(user_id);

console.log(users.storyMute)
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You may use Array.prototype.reduce() the following way:

const users=[{_id:"5fbfa729fc46a415ce5503a6",first_name:"Allen",last_name:"Border",timestamp:1606395689121,key:["5fbf6f91aff7f3320a906547","5fbfa748fc46a415ce5503a8"]},{_id:"5fbfa6fbfc46a415ce5503a4",first_name:"james",last_name:"roger",timestamp:1606395689125},{_id:"5fbf6f91aff7f3320a906547",first_name:"david",last_name:"gosh",timestamp:1606395689130,key:["5fbfa729fc46a415ce5503a6"]},{_id:"5e4e74eb380054797d9db623",first_name:"Ricky",last_name:"bichel",timestamp:1606395689131}]
const user_id="5fbfa748fc46a415ce5503a8"

const {matching, nonMatching} = users.reduce((acc, userRecord) => {
  userRecord?.key?.includes(user_id)
  // or, if you need to compare by _id
  // userRecord._id === user_id
    ? acc.matching.push(userRecord)
    : acc.nonMatching.push(userRecord)
  return acc
}, {matching: [], nonMatching: []})

console.log(matching)

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