Remove a value from an array

I'm making an inventory system, and I decided to use arrays, but I haven't figured out how to remove something from it. Let's say there is an array [1, 2, 3, 4, 5] , and I need to remove "4" from it so that the result is [1, 2, 3, 5]
Hi there!

You can use the method .splice

JavaScript:
const myArray = [1, 2, 3, 4, 5]
const index = myArray.indexOf(ELEMENT)
if(index > -1){
    myArr.splice(index, 1)
}

Replace ELEMENT with the one you want to remove.
RPG Maker adds a "remove" method that we can use.

array.remove(4) removes 4 from the array, not the 4-th element.

EDIT:
MZ only!
Thanks, this is what I was looking for. but now the question is whether it is possible to find an element in the array and write its index as a variable.

so that the game knows which element to remove
Thanks, this is what I was looking for. but now the question is whether it is possible to find an element in the array and write its index as a variable.

Code:
const value = yourArray.indexOf(4)
$gameVariables.setValue(1, value)
Get index of your array and put it into Game Variable 1

so that the game knows which element to remove
Don't mix index and value!

When you want to remove number 4 from your array: array.remove(4)
When you want to remove the 4-th element from your array: array.remove(array[4])
RPG Maker adds a "remove" method that we can use.
Are you sure that's a thing in MV? Because it doesn't seem to work for me
1719749191902.png

Nor can I find anything creating a function called "remove" for arrays.
This was posted in the MV Support Section, but the OP has MZ listed as Primarily Uses.

In case it helps for MZ users, I just did a quick test of this because I hadn't seen or used this before. And it seems to work perfectly.

Array Remove Test.png

Not sure about MV.

I have definitely added this to my Research Notes of reminders for how to do things. Up to this point, I think I have been using .splice.
Are you sure that's a thing in MV? Because it doesn't seem to work for me
View attachment 309956
Nor can I find anything creating a function called "remove" for arrays.
I'm no JS expert, and my MV is uninstalled atm, but in MZ I got the above to work using this:

JavaScript:
let myArray = [1,2,3,4,5,6,7];
myArray.remove(4);
console.log(myArray);

I suspect the error is coming from having the actual array in the remove command. You should be removing the index you want from the variable your array is stored in.

But as I said, I can only try MZ atm.

@Aerosys Thank you. I wasn't aware of this function.

*edit*
@Mac15001900 I got curious and re-installed MV and the above code I used in MZ does NOT work in MV.
Are you sure that's a thing in MV? Because it doesn't seem to work for me
View attachment 309956
Nor can I find anything creating a function called "remove" for arrays.
You are correct - it is not in the corescripts of MV. It is defined in MZ (JS Extensions):

JavaScript:
/**
 * Removes a given element from the array (in place).
 *
 * @memberof JsExtensions
 * @param {any} element - The element to remove.
 * @returns {array} The array after remove.
 */
Array.prototype.remove = function(element) {
    for (;;) {
        const index = this.indexOf(element);
        if (index >= 0) {
            this.splice(index, 1);
        } else {
            return this;
        }
    }
};

Object.defineProperty(Array.prototype, "remove", {
    enumerable: false
});
Ah sorry, I could swear it was already added in MV.

Original thread: https://forums.rpgmakerweb.com/threads/remove-a-value-from-an-array.169910/