[Solved] Parse comma separated string, split array into chunks, then transpose

So long as the number of rows is known, it only takes: a regex to split the string on comma-spaces which immediately follow a one-or-more digits or a word starting with an uppercase letter, a call of array_chunk() and array_map() (with a null callback and data spreading) to “transpose” the data. Code: (Demo) $string = … Read more

[Solved] Group array of objects into deeper parent-child structure

not really sure the purpose of this exercise but this gets your desired result with the given data $objects = [ (object) [‘id’=> 1, ‘value’ => 0], (object) [‘id’=> 2, ‘value’ => 10], (object) [‘id’=> 3, ‘value’ => 14], (object) [‘id’=> 4, ‘value’ => 0], (object) [‘id’=> 5, ‘value’ => 21], (object) [‘id’=> 6, ‘value’ … Read more

[Solved] JavaScript sorting and grouping items in array

Use objects for the grouping. var categories = {}; function incrementBy(category, value) { if (!categories[category]) { categories[category] = 0; } categories[category] += value; } var datasetarr = []; var pos; for(var i = 0; i < arr.length; i++){ console.log(‘unsorted ‘ + arr[i].type + ‘ ‘ + arr[i].quantity); incrementBy(arr[i].type, arr[i].quantity) } for(var category in categories){ console.log(‘sorted … Read more