Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hjortur17's avatar

How to remove from array

Hi, I have an array that's hold prices for selected prices, and a checkbox to see if we should add to the array or not. But I'm having that issue of if you click the box and then unclick it the price wound reduce. Any ideas how to fix that? Here is my method:

servicesToArray(object) {
    if (this.checked = true) {
        this.selectedServicesDesc.push(object.description);
        this.selectedServicesPrices.push(object.price);
    }

    if (this.checked = false) {
        this.selectedServicesPrices.delete(object.price, 1);
    }
}
<input class="mr-2 leading-tight" type="checkbox" v-model="checked" @click="servicesToArray(service)">
<input class="mr-2 leading-tight" type="checkbox" :value="service.description" hidden>
<span class="font-normal text-white" v-text="service.description"></span>
0 likes
4 replies
tykus's avatar

You are assigning true to this.checked here:

this.checked = true

So that block will always be executed.

delete is not an Array method; what is this.selectedServicesPrices

hjortur17's avatar

If I do this like this:

if (this.checked) {
                    this.selectedServicesDesc.push(object.description);
                    this.selectedServicesPrices.push(object.price);
                } else {
                    this.selectedServicesDesc.delete(object.description);
                    this.selectedServicesPrices.delete(object.price);
                }
<input class="mr-2 leading-tight" type="checkbox" v-model="checked" @click="checked = true, servicesToArray(service)">

It's still not removing it from the array. Any better idea how to do this?

hjortur17's avatar

My data:

checked: false,

                services: [],
                selectedServicesDesc: [],
                selectedServicesPrices: [],
hjortur17's avatar
hjortur17
OP
Best Answer
Level 14

Figured it out:

servicesToArray(object) {
    if (this.checked) {
        if (this.selectedServicesPrices.includes(object.price) && this.selectedServicesDesc.includes(object.description)) {
            this.selectedServicesDesc.splice(object, 1);
            this.selectedServicesPrices.splice(object, 1);
        } else {
            this.selectedServicesDesc.push(object.description); 
            this.selectedServicesPrices.push(object.price);
        }
    }
}

Please or to participate in this conversation.