Skip to content

Instantly share code, notes, and snippets.

@prio101
Last active November 25, 2021 05:00
Show Gist options
  • Save prio101/9dece1570dfed1c86f4db99a709c412e to your computer and use it in GitHub Desktop.
Save prio101/9dece1570dfed1c86f4db99a709c412e to your computer and use it in GitHub Desktop.
Group by the n spice
// Idea:
// get the main result set into:
// three seperate list in a main list: [[1,2,3], [1,2], [1,2....,n]]
// Maybe here in this case js will not clone the main list .
// it will manipulate the main list. Attention to it.
list = [1,2,3,4,5,6,7,8,9]
const group_by_n = (list) => {
// same as before:
let temp_list = []
let group_base_spice = 3
let group_base_spice_special_case_secon_row = 2
let result_list = []
for(item in list){
if(item == 0 && list.length > group_base_spice){
temp_list = list.splice(0, group_base_spice);
result_list.push(temp_list);
temp_list = [] // reset
group_base_spice = group_base_spice_special_case_secon_row
}
if(item == 1 && list.length > group_base_spice){
temp_list = list.splice(0, group_base_spice);
result_list.push(temp_list);
temp_list = []
}
// guess this line will throw error
// before: result_list.append(list)
// after: only add the rest of item
result_list.push(list[item])
console.log("this should show three seperate list:",result_list)
return result_list;
}
}
@prio101
Copy link
Author

prio101 commented Nov 24, 2021

result should be = [[1,2,3], [4,5], [rest....n]]

now run a loop on the view file to get the sets as grouped.

like: while 1 -> 2 do this.

for row 3 just print rest or paginate .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment