I need to send data to the server that has multiple ids with sub ids in it. Currently I am using arrays but because I need to handle each id and sub ids on the dom (currently with vue) I use a lot of find() and findIndex() again and again. If there is a way to use ids directly I just could access or check them with order[id][subid].
Currently I am using this structure:
let data = {
orders: [
{ order_id: 4, items: [{ item_id: 6 }, { item_id: 7 }, { item_id: 8 }] },
{ order_id: 1, items: [{ item_id: 1 }, { item_id: 2 }, { item_id: 3 }] },
];
}
Here when I for example want yo check if a orders item is checked I have to
function check(order_id, item_id) {
let order_i = data.orders.findIndex((o) => o.order_id == order_id);
if (order_i != -1) {
let item_i = data.orders[order_i].items.findIndex((o) => o.item_id == item_id);
}
if (item_i != -1) {
return true;
} else {
return false;
}
}
or something like that. What I want to use it like
let data = {
orders: {
4: {6:'',7:'',8:''},
1: {1:'',2:'',3:''}
}
}
and then I would just use data.orders?.[order_id]?.[item_id]
I am not sure would that be a good approach. Doesn't look so.
What structure could I use for this, without using arrays ?