admin管理员组文章数量:1335430
I tried couple of way but not getting the result as expected.
Say that I have array of objects like this:
var users = [
{ 'user': 'barney', 'age': 36, 'active': false },
{ 'user': 'fred', 'age': 40, 'active': true },
... and so on (n lenght) ...
];
I wanted to divide them into two or three groups. If I have 100 objects and I like them to be divided in 3 groups, then the result with first and second group should contain 33 objects and the last group should contain 34 objects (or any other best possible way remaining objects distributed).
I tried to use lodash's chunk but that does things pletly different :)
console.log(_.chunk(users, 2)) // this will create 50 chunks of 2 objects
Edited my question to explain more.
var my arrayOfObj = [1,2,3,4,5,6,7,8,9,10]
myArray.dividThemIn(3) // when run this, I am getting chunks like below
[{1,2,3}, {4,5,6}, {7,8,9}, {10}] // I am finding code that does this
[{1,2,3,4}, {5,6,7,8}, {9,10}] // But I need the code that does this
I tried couple of way but not getting the result as expected.
Say that I have array of objects like this:
var users = [
{ 'user': 'barney', 'age': 36, 'active': false },
{ 'user': 'fred', 'age': 40, 'active': true },
... and so on (n lenght) ...
];
I wanted to divide them into two or three groups. If I have 100 objects and I like them to be divided in 3 groups, then the result with first and second group should contain 33 objects and the last group should contain 34 objects (or any other best possible way remaining objects distributed).
I tried to use lodash's chunk but that does things pletly different :)
console.log(_.chunk(users, 2)) // this will create 50 chunks of 2 objects
Edited my question to explain more.
var my arrayOfObj = [1,2,3,4,5,6,7,8,9,10]
myArray.dividThemIn(3) // when run this, I am getting chunks like below
[{1,2,3}, {4,5,6}, {7,8,9}, {10}] // I am finding code that does this
[{1,2,3,4}, {5,6,7,8}, {9,10}] // But I need the code that does this
Share
Improve this question
edited Feb 14, 2018 at 22:53
Dexygen
12.6k13 gold badges86 silver badges151 bronze badges
asked Feb 14, 2018 at 18:02
SyedSyed
16.5k14 gold badges126 silver badges157 bronze badges
4
- Possible duplicate of Split array into chunks – I wrestled a bear once. Commented Feb 14, 2018 at 18:04
- Please do some research before asking.. there are at least a few duplicates of this exact question on SO. – I wrestled a bear once. Commented Feb 14, 2018 at 18:05
- @Occam'sRazor, I did research, either I am doing something wrong or for my luck I am not getting I want. Anyhow, Edited my question to explain more, kindly have a look. – Syed Commented Feb 14, 2018 at 20:28
- @GeorgeJempty - How is it more than chunking an array? it's an exact duplicate as far as I can tell.. this is the code given in the other answer, applied to his example and it provides the exact requested results. - maybe if i knew what the lodash chunk function did i would understand the question better – I wrestled a bear once. Commented Feb 14, 2018 at 20:49
3 Answers
Reset to default 8Divide the length of the array by the number of chunks you want (rounding up), and pass that into _.chunk
as the second parameter:
var arrayOfObj = [1,2,3,4,5,6,7,8,9,10];
var chunks = _.chunk(arrayOfObj, Math.ceil(arrayOfObj.length / 3));
console.log(JSON.stringify(chunks)); //[[1,2,3,4],[5,6,7,8],[9,10]]
If you just want to split them into 3 groups without any criteria.
function splitIntoThree(arr){
let result = [[],[],[]],
thirds = Math.ceil(arr.length/3);
arr.forEach((obj,index) => {
if(index < thirds){
result[0].push(obj);
} else if(index < 2*thirds) {
result[1].push(obj);
} else {
result[2].push(obj);
}
});
return result;
}
You could use something like this.
function chunk (arr, chunks) {
var chunked = [];
var itemsPerChunk = Math.ceil(arr.length / chunks);
while (arr.length > 0) {
chunked.push(arr.slice(0, itemsPerChunk));
}
return chunked;
}
var data = [1,2,3,4,5,6,7,8,9,10];
chunk(data, 3);
// [[1,2,3,4], [5,6,7,8], [9,10]]
本文标签:
版权声明:本文标题:javascript - How to partition or divide an array (of objects) into specific number of chunks, possibly using lodash - Stack Over 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742378199a2463587.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论