1

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

3
  • 2
    what should be your expected result, Please add in the question itself... Commented Oct 16, 2021 at 4:25
  • Did that above! be good to remove a number essentially Commented Oct 16, 2021 at 4:40
  • That's good but you didn't mention which number should be removed. You have removed 14, Is there any criteria? Commented Oct 16, 2021 at 4:42

2 Answers 2

1

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)

Sign up to request clarification or add additional context in comments.

3 Comments

Brilliant, would this still work with the numbers for times in the array?
I didn't get your question, do you want to filter out/edit elements based on the values in the scheduledtimes array?
@LeCoda, ok, I see the update of your post. I added an example of Mana2 where I removed the number 14. You'd want to separate the logic to modify each type of power in different functions to organize better your code
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"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.