How to remove an item from an array?

Hi guys,

I know that we add an item to an array like this

var animals = ['pigs', 'goats', 'sheep'];

console.log(animals.push('cows'));
// expected output: 4

console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]

Can anyone help me with removing an item from an array.

Thank you

You can use splice for that purpose. To play off of your example:

var animals = [‘pigs’, ‘goats’, ‘sheep’];
animals.push(‘cows’)
// now, take out goats, the second item in the array
animals.splice(1,1);
console.log('First item in array after goats was removed: ’ + animals[0]);
console.log('Second item in array after goats was removed: ’ + animals[1]);
console.log('Third item in array after goats was removed: ’ + animals[2]);

Isn’t there a way to remove the item by its name?

like this

array.splice($item("#name").text));

I am pushing the items into the array with checkboxes, see below

export async function checkbox_change(event) {
 let $item = $w.at(event.context);
 if ($item("#checkbox").checked === true) {
 await console.log(array.push($item("#id").text));
        console.log(array);
    }
 else if ($item("#checkbox").checked === false) {
 
    }
}

I need to remove the item if the person comes back and un-checks the box.

Is there any work around?

Shan,

A common way to do this is to obtain the index of the item to remove with the indexOf function. Now that you know the index of it, apply that variable to the splice function.

let index = animals.indexOf(‘goats’);
animals.splice(index,1);

This worked for me. I set a unique _id when sending the item to the array and used he same _id to remove it.

async function removePType($item, params) {
    removeByKey({
        key: '_id',
        value: $item("#variable").text.toString()
    });
}

function removeByKey(params){
    array.some(function(item, index) {
 if(array[index][params.key] === params.value){
            array.splice(index, 1);
            console.log(array);
        }
    });
}

Hi Shan. I’m trying to do the same. I’ve added in the array, but I’m not sure how to remove the item. I’m using a checkbox to see whether to add or remove. Can you explain a little bit? My code:

let secondRepeaterData = [];
function selectItem() {

let itemToAdd = {}; 
let sumTotal = 0; 

$w("#selectItem").onChange((event) => { 
    const $item = $w.at(event.context); 

    if ($item("#selectItem").checked) { 
        let finalPrice = $item("#price").text * $item("#dropdown1").value; 
        $item("#ifMarked").text = finalPrice.toString(); 
        
                    let id1 = $item('#title').text; 
                    let id11 = id1.replace(/\s/g, ""); 
                    let price = $item('#price').text; 
                    let marked = $item('#ifMarked').text; 
                    let qty = $item('#dropdown1').value; 

                    itemToAdd = { 
                        "_id": id11, 
                        "price": price, 
                        "qty": qty, 
                        "title1": id1, 
                        "ifMarked": marked 
                    }; 
                    secondRepeaterData.push(itemToAdd); 
                    console.log(secondRepeaterData); 
                    

    } else //if (!($item("#selectItem").checked))  
    { 
        $item("#ifMarked").text = "0"; 
        
        let id1 = $item('#title').text; 
        let id11 = id1.replace(/\s/g, ""); 

        async function removePType($item1, params) { 
            removeByKey({ 
                key: id11, 
                value: id11 
                
            }); 
        
        } 

        function removeByKey(params) { 
            console.log("wo"); 
            secondRepeaterData.some(function (item, index) { 
                if (secondRepeaterData[index][params.key] === params.value) { 
                    secondRepeaterData.splice(index, 1); 
                    console.log(secondRepeaterData); 
                } 
            }); 
        } 
        
    } 

}); 

}