How can I use for loops to create multiple variables with JS?

I want to save a series of array data. When I trigger the click () event, I will store this data in the array, but only one array can be created, so every time click is triggered, I store the data in the same array. I want to distinguish different data in different arrays, but I cannot create them manually, because the data is generated dynamically. I"m not sure how many variables I need. This is probably the case.
as shown in the following figure

,
:
,
,,
[{floor: 1, roomNos: ["102", "103"]}, {floor: 2, roomNos: ["204"]}]


current code:

    <div id="son_house_room_number_btns">
        

1

<div class="weui-flex"> <div class="weui-flex__item"> <div class="placeholder"> <a href="javascript:;" class="weui-btn weui-btn_disabled weui-btn_default">101</a> </div> </div> <div class="weui-flex__item"> <div class="placeholder"> <a href="javascript:;" class="weui-btn weui-btn_disabled weui-btn_default">102</a> </div> </div> <div class="weui-flex__item"> <div class="placeholder"> <a href="javascript:;" class="weui-btn weui-btn_disabled weui-btn_default">103</a> </div> </div> </div>

2

<div class="weui-flex"> <div class="weui-flex__item"> <div class="placeholder"> <a href="javascript:;" class="weui-btn weui-btn_disabled weui-btn_default">201</a> </div> </div> <div class="weui-flex__item"> <div class="placeholder"> <a href="javascript:;" class="weui-btn weui-btn_disabled weui-btn_default">202</a> </div> </div> <div class="weui-flex__item"> <div class="placeholder"> <a href="javascript:;" class="weui-btn weui-btn_disabled weui-btn_default">203</a> </div> </div> <div class="weui-flex__item"> <div class="placeholder"> <a href="javascript:;" class="weui-btn weui-btn_disabled weui-btn_default">204</a> </div> </div> </div> </div> selectRoomNoList = []; arr=[] $("-sharpson_house_room_number_btns>.son_house_room_number_area_div>.weui-flex>.weui-flex__item>.placeholder>.weui-btn").click(function() { // var btn_first = $(this).attr("class"); // var btn_zdy=$(this).attr("data-index"); // console.log(btn_zdy) if(btn_first == "weui-btn weui-btn_disabled weui-btn_default") { $(this).attr("class", "weui-btn weui-btn_plain-primary"); // var floor_p = $(this).parent().parent().parent().siblings("p").children("a"); // selectRoomNoList.push($(this).text()); arr.push($(this).text()); selectRoomNoList.push({floorNo:floor_p.text(),rooms:arr}); console.log(selectRoomNoList) } else { $(this).attr("class", "weui-btn weui-btn_disabled weui-btn_default"); // , null // indexOf: selectRoomNoList[selectRoomNoList.indexOf($(this).text())] = null; // splice null // splice(:,/, :,) selectRoomNoList.splice(selectRoomNoList.indexOf(null), 1); console.log(selectRoomNoList) } });

many methods have been tried but have not been solved, or is there a better way to solve this problem?

Sep.18,2021

dynamic generation can only eval concatenate variable names, but these variables have to be defined in advance
this is not advisable. What are you going to do?
['101','102',"103","201","204"].reduce((s,v)=>{
    var _tag = v[0];
    var index = s.findIndex(val=>val.floor == _tag);
    if(!~index){
        index = s.length;
         s.push({
            floor: _tag,
            roomNos:[]
        })
    }
    s[index].roomNos.push(v)
    return s
}, [])

= Update

var selectRoomList = []; // ii

function updateFloor(floor, rooms){
    if(selectRoomList[floor]){
        // undefined
        selectRoomList[floor].rooms = rooms;
    }else{
        selectRoomList[floor] = {
            floorNo: floor,
            romms: rooms
        };
    }
}

// 
function getPurifiedData(selectRoomList){
    return selectRoomList.filter(item => {
        return !!item;
    });
}

= original answer
Why do you have to create so many variables? you can create an array;
or you can create an object and dynamically add properties under the object, or, depending on your requirements

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b41c52-2c5d5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b41c52-2c5d5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?