Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

66
Visualizações
filter array of object in javascript based on conditions?

I'm trying to filter an array in javascript for a simple form in react.

The array looks like this,

var Details = [
  {
    Magic: "Mana",
    scheduledtimes: [8,14],
    startdate: "1996-12-02",
  },
  {
    Magic: "Vispasion",
    scheduledtimes: [8],
    startdate: "1996-12-02",
  },
  ...
];

I want to simply find the correct magic power, then be able to remove values or remove the object if the magic power is chosen to be removed.

This removes the object totally,

var Test = Details.find((v) => v.Magic === "Mana")
  ? Details.filter((v) => v.Magic !== "Mana")
  : [...Details, "Mana"];

However, when I'm not sure how to simply remove the time values, or the string value, but keep the object the same overall?

The expect result would be, say if i removed one specific time value,

var Details = [
  {
    Magic: "Mana",
    scheduledtimes: [8],
    startdate: "1996-12-02",
  },
  {
    Magic: "Vispasion",
    scheduledtimes: [8],
  },

Trying to remove the start date parameter now, having a bit of trouble

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

What I understood is that you want to edit or remove an object in the array depending on some condition, you could achieve it using reduce.

var Details = [{
    Magic: "Mana",
    scheduledtimes: [8],
    startdate: "1996-12-02",
  },
  {
    Magic: "Mana2",
    scheduledtimes: [8, 14],
    startdate: "1996-12-02",
  },
  {
    Magic: "Vispasion",
    scheduledtimes: [8],
    startdate: "1996-12-02",
  },
  {
    Magic: "Untouched",
    scheduledtimes: [8],
    startdate: "1996-12-02",
  },
];

var Test = Details.reduce((acc, power) => {
  if (power.Magic === "Mana") return acc; // remove mana powers
  if (power.Magic === "Mana2") {
    // remove 14 in times for Mana2 powers
    return [...acc, { ...power,
      scheduledtimes: power.scheduledtimes.filter(time => time !== 14)
    }]
  }
  if (power.Magic === "Vispasion") {
    // modify the power by concatenating an new object with partial info
    return [...acc, {
      Magic: power.Magic
    }];
  }
  return [...acc, power]; // leave the power in the array by default
}, []);

console.log(Test)

about 4 years ago · Juan Pablo Isaza Relatório

0

In your condition, you just need to return what you want

var Test = Details.find(v=> v.Magic === "Mana") ? Details.filter(v => { if(v.Magic !== "Mana") return { return {Magic: a.Magic} }) : [...Details, "Mana"]
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda