MZ How to get an array of items in insertion order

Hi everyone, I'm working on an item bar window. I realized that in the code, "$gameParty._items" is saved as type "Object".
When I get the list of items from "Game_Party.prototype.items()", it returns me an array of items in ascending order of their IDs.

JavaScript:
Game_Party.prototype.initAllItems = function() {
    this._items = {};
    this._weapons = {};
    this._armors = {};
};
JavaScript:
Game_Party.prototype.items = function() {
    return Object.keys(this._items).map(id => $dataItems[id]);
};
JavaScript:
Game_Party.prototype.gainItem = function(item, amount, includeEquip) {
    const container = this.itemContainer(item);
    if (container) {
        const lastNumber = this.numItems(item);
        const newNumber = lastNumber + amount;
        container[item.id] = newNumber.clamp(0, this.maxItems(item));
        if (container[item.id] === 0) {
            delete container[item.id];
        }
        if (includeEquip && newNumber < 0) {
            this.discardMembersEquip(item, -newNumber);
        }
        $gameMap.requestRefresh();
    }
};

Since I want to get a list of items sorted in insertion order, I added an “_orderItems” attribute to Game_Party, it is an attribute of type Map. I assign it a value in the "gainItem" method, it's essentially just an insertion-ordered copy of "_items".

Before I tried to save the game, this method accomplished what I was looking for. But when I save the game and then wish to continue it, I found an error in the game. "_orderItems" becomes "{}", so I checked the MZ save game method,

JavaScript:
JsonEx.stringify = function(object) {
    return JSON.stringify(this._encode(object, 0));
};
JavaScript:
JsonEx._encode = function(value, depth) {
    // [Note] The handling code for circular references in certain versions of
    //   MV has been removed because it was too complicated and expensive.
    if (depth >= this.maxDepth) {
        throw new Error("Object too deep");
    }
    const type = Object.prototype.toString.call(value);
    if (type === "[object Object]" || type === "[object Array]") {
        const constructorName = value.constructor.name;
        if (constructorName !== "Object" && constructorName !== "Array") {
            value["@"] = constructorName;
        }
        for (const key of Object.keys(value)) {
            value[key] = this._encode(value[key], depth + 1);
        }
    }
    return value;
};

I searched on Google and found that "JSON.stringify" doesn't support "Map" type objects, and it seems that "JsonEx._encode" can't handle the "Map" type. Has anyone ever implemented getting a list of items in insertion order? Any suggestions are welcome!
Since you want an array (a list), why don't you just make an array?

Code:
var oldSystemInitialize = Game_System.prototype.initialize;
Game_System.prototype.initialize = function()
{
    oldSystemInitialize.call(this);
   this.itemsGained = [];
};

Then in gainItem(), just inside the if (container) you put
Code:
if (amount > 0)
    $gameSystem.itemsGained.push(item.id);

Then you have $gameSystem.itemsGained as an array of all items gained in order, and the saving is taken care of for you automatically.
Thanks for your reply. This should work, saving an array of item IDs in the order in which they were inserted.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-get-an-array-of-items-in-insertion-order.170153/