I have an object with some properties like:
const rows_obj = {
prova1:[{price:3,description:"test11"},{price:7,description:"test12"}],
prova2:[{price:11,description:"test21"},{price:2,description:"test22"}],
prova3:[{price:1,description:"test31"},{price:23,description:"test32"}],
}
and i need to print the rows in one or more pages, so the limit per page is for instance 3 rows. So in this scenario, I need to have an array of object obj like:
obj[0] = {
total:21,
prova1:[{price:3,description:"test11"},{price:7,description:"test12"},],
prova2:[{price:11,description:"test21"}],
}
obj[1] = {
total:26,
prova2:[{price:2,description:"test22"}],
prova3:[{price:1,description:"test31"},{price:23,description:"test32"},],
}
(Since in this case the limit is 3 rows per page/object)
But the limit could be also 20 rows so the final object will be:
obj[0] = {
total:47,
prova1:[{price:3,description:"test11"},{price:7,description:"test12"},],
prova2:[{price:11,description:"test21"},{price:2,description:"test22"},],
prova3:[{price:1,description:"test31"},{price:23,description:"test32"},],
}
Because in the original object there are 6 rows, then, since is under the limit, the function has to retrive an array with one element and this one element is equal to the original one.
I tried but so far i have made this code:
const size = 3
const rows_obj = {
prova1:[{price:22,description:"test11"},{price:23,description:"test12"},],
prova2:[{price:22,description:"test21"},{price:23,description:"test22"},],
prova3:[{price:22,description:"test31"},{price:23,description:"test32"},],
}
var rows_length = 0;
for(var char in rows_obj){
// Confirm that the key value is an array before adding the value.
if(Array.isArray(rows_obj[char])){
rows_length += rows_obj[char].length;
}
}
if (!rows_length) {
return [[]]
}
const arrays = []
let i = 0
const keys = Object.keys(rows_obj)
let obj = null
while (i<rows_length) {
obj = {}
for(let j=0;j<keys.length;j++) {
obj[keys[j]] = rows_obj[keys[j]].slice(i, i + size)
i = i + 2 + size
console.log(i)
}
arrays.push(obj)
}
And it is not working, i'm doing a mess... any help? thank you in advance.