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

121
Views
Splice on computed item that maps elements

I'd like to ensure that all objects in an array are of type Item. If I do so, a splice does no longer work later.

This is how I do this:

computedItems: {
    get()
    {
        //return this.modelValue;
        return this.modelValue?.map((item) => new Item(item));
    },
    set(newValue)
    {
        this.$emit("update:modelValue", newValue);
    }
}

This works fine but it seems to break reactivity, as:

removeItem(item) {
    let key = this.computedItems.findIndex((i) => {
        return item === i;
    });

    this.computedItems.splice(key, 1);
}

does not work (no error, list is just not being updated).

If I do

computedItems: {
    get()
    {
        return this.modelValue;
    },
    set(newValue)
    {
        this.$emit("update:modelValue", newValue);
    }
}

the splice does work as expected (but the items are not mapped to the specific object).

My questions:

  1. How can I solve that?
  2. Why is that the case? Is it a bad idea to map within the computed setter?
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I'm not sure why it happens and cannot explain it very well.

Array and Object is keeping in memories. So it’s a pointer to memories. The pointer was not changing and nothing is reactive. (Headache)

Can you try this code:

removeItem(item) {

    const newArray = [...this.computedItems];
    let key = newArray.findIndex(i => item === i);

    newArray.splice(key, 1);

    this.computedItems = newArray;
}
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!