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

150
Vistas
compare two array and put a status in an other array if it exist or not

so i have an array with all the players and one with only the one that are selected and i want to have an other array with the status if he is selected or not. i tried comparing and pushing the element with the status but didnt achieve what i wanted.

here are the arrays

  const all = [
    {
      playerId: '294',
      firstName: 'MMM',
    },
    {
      playerId: '295',
      firstName: 'arkiv',
    },
    {
      playerId: '296',
      firstName: 'julio',
    },
    {
      playerId: '297',
      firstName: 'sss',
    },
  ];

const selected = [
    {
      playerId: '296',
      firstName: 'julio',
    },
    {
      playerId: '297',
      firstName: 'sss',
    },
  ];

and this is what i want to achive

  const res = [
    { playerId: '294', firstName: 'MMM', status: false },
    { playerId: '295', firstName: 'arkiv', status: false },
    { playerId: '296', firstName: 'julio', status: true },
    { playerId: '297', firstName: 'sss', status: true },
  ];

i set up an environment to work here: https://stackblitz.com/edit/react-lkcqcd?file=src%2FApp.js

thanks for attention!

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

0

You can use Array#some to check if the id of the player is in the selected array.

const all=[{playerId:'294',firstName:'MMM',},{playerId:'295',firstName:'arkiv',},{playerId:'296',firstName:'julio',},{playerId:'297',firstName:'sss',},],selected=[{playerId:'296',firstName:'julio',},{playerId:'297',firstName:'sss',},];
all.forEach(player => player.status = 
              selected.some(x => x.playerId === player.playerId));
console.log(all);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can create a Set of selected players and using this set you can add the status field.

const 
  all = [{ playerId: "294", firstName: "MMM" }, { playerId: "295", firstName: "arkiv" }, { playerId: "296", firstName: "julio" }, { playerId: "297", firstName: "sss" }],
  selected = [{ playerId: "296", firstName: "julio" }, { playerId: "297", firstName: "sss" }],
  selectedSet = new Set(selected.map((player) => player.playerId)),
  res = all.map((player) => ({ ...player, status: selectedSet.has(player.playerId) }));

console.log(res);

You can also use Array.prototype.find instead of creating a set but this wouldn't be optimal unless the size of the selected array is quite small.

const 
  all = [{ playerId: "294", firstName: "MMM" }, { playerId: "295", firstName: "arkiv" }, { playerId: "296", firstName: "julio" }, { playerId: "297", firstName: "sss" }],
  selected = [{ playerId: "296", firstName: "julio" }, { playerId: "297", firstName: "sss" }],
  res = all.map((player) => ({
    ...player,
    status: !!selected.find((selPlayer) => selPlayer.playerId === player.playerId),
  }));

console.log(res);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use Array#map and Array#includes as in the following demo:

const all = [ { playerId: '294', firstName: 'MMM', }, { playerId: '295', firstName: 'arkiv', }, { playerId: '296', firstName: 'julio', }, { playerId: '297', firstName: 'sss', }, ],

      selected = [ { playerId: '296', firstName: 'julio', }, { playerId: '297', firstName: 'sss', }, ],

      output = all.map(
          player => 
          ({
              ...player,
              status:selected.map(({playerId}) => playerId)
              .includes(player.playerId)
          })
      );

console.log( output );

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