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]
Remove a value from an array
RPG Maker MV SupportHi there!
You can use the method .splice
developer.mozilla.org
Replace ELEMENT with the one you want to remove.
You can use the method .splice
Array.prototype.splice() - JavaScript | MDN
The splice() method of Array instances changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
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.
EDIT:
MZ only!
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
so that the game knows which element to remove
Code:
const value = yourArray.indexOf(4)
$gameVariables.setValue(1, value)
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])Are you sure that's a thing in MV? Because it doesn't seem to work for me
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.

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.
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.

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.
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);
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.
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/