Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

67
Views
filtrar la matriz de objetos en javascript según las condiciones?

Estoy tratando de filtrar una matriz en javascript para una forma simple en reaccionar.

La matriz se ve así,

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

Simplemente quiero encontrar el poder mágico correcto, luego poder eliminar valores o eliminar el objeto si se elige eliminar el poder mágico.

Esto elimina el objeto totalmente,

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

Sin embargo, cuando no estoy seguro de cómo simplemente eliminar los valores de tiempo o el valor de cadena, pero mantener el objeto igual en general.

El resultado esperado sería, por ejemplo, si eliminé un valor de tiempo específico,

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

Intentando eliminar el parámetro de fecha de inicio ahora, teniendo algunos problemas

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Lo que entendí es que desea editar o eliminar un objeto en la matriz según alguna condición, puede lograrlo usando 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 Report

0

En su condición, solo necesita devolver lo que quiere

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!