JavaScript将数组拆分为内部的多个数组[重复]
Posted
技术标签:
【中文标题】JavaScript将数组拆分为内部的多个数组[重复]【英文标题】:JavaScript Split array into multiple arrays inside [duplicate] 【发布时间】:2020-10-20 12:17:54 【问题描述】:如何使用 javascript 拆分以下数组? 我在这里要做的是将这个数组拆分为这个数组内的多个数组,这些数组应该是唯一的 1、2、3、4、5 个候选配置 ID
0: candidateConfigId: "1", value: "199128700790"
1: candidateConfigId: "2", value: "Yujith"
2: candidateConfigId: "3", value: "Male"
3: candidateConfigId: "4", value: "SE"
4: candidateConfigId: "5", value: "Colombo5"
5: candidateConfigId: "1", value: "199128700792"
6: candidateConfigId: "2", value: "Yujith2"
7: candidateConfigId: "3", value: "Male"
8: candidateConfigId: "4", value: "SE"
9: candidateConfigId: "5", value: "Colombo3"
10: candidateConfigId: "1", value: "199128700793"
11: candidateConfigId: "2", value: "Yujith3"
12: candidateConfigId: "3", value: "Male"
13: candidateConfigId: "4", value: "SE"
14: candidateConfigId: "5", value: "Colombo2"
预期输出
[
[
0: candidateConfigId: "1", value: "199128700790"
1: candidateConfigId: "2", value: "Yujith"
2: candidateConfigId: "3", value: "Male"
3: candidateConfigId: "4", value: "SE"
4: candidateConfigId: "5", value: "Colombo5"
],
[
0: candidateConfigId: "1", value: "199128700792"
1: candidateConfigId: "2", value: "Yujith2"
2: candidateConfigId: "3", value: "Male"
3: candidateConfigId: "4", value: "SE"
4: candidateConfigId: "5", value: "Colombo3"
],
[
10: candidateConfigId: "1", value: "199128700793"
11: candidateConfigId: "2", value: "Yujith3"
12: candidateConfigId: "3", value: "Male"
13: candidateConfigId: "4", value: "SE"
14: candidateConfigId: "5", value: "Colombo2"
]
]
Thanks in advance
【问题讨论】:
数组是否按上述排序?你试过什么? 【参考方案1】:您可以获取所需 id 的数组和存储同一组结果集索引的对象。
var data = [ candidateConfigId: "1", value: "199128700790" , candidateConfigId: "2", value: "Yujith" , candidateConfigId: "3", value: "Male" , candidateConfigId: "4", value: "SE" , candidateConfigId: "5", value: "Colombo5" , candidateConfigId: "1", value: "199128700792" , candidateConfigId: "2", value: "Yujith2" , candidateConfigId: "3", value: "Male" , candidateConfigId: "4", value: "SE" , candidateConfigId: "5", value: "Colombo3" , candidateConfigId: "1", value: "199128700793" , candidateConfigId: "2", value: "Yujith3" , candidateConfigId: "3", value: "Male" , candidateConfigId: "4", value: "SE" , candidateConfigId: "5", value: "Colombo2" ],
ids = ['1', '2', '3', '4', '5'],
result = data
.reduce((r, o) =>
let index = r.indices[o.candidateConfigId]++;
r.data[index] = r.data[index] || [];
r.data[index].push(o);
return r;
, indices: Object.fromEntries(ids.map(k => [k, 0])), data: [] )
.data;
console.log(result);
.as-console-wrapper max-height: 100% !important; top: 0;
【讨论】:
@OP 只是为了让您了解它的作用(不同),它允许您的 id 完全乱序。这意味着您可以拥有[id:1,id:1,id:2,id:1,id:3...]
,它仍然会将唯一ID 分块到数组[ [id:1,id:2,id:3...], [id:1,...], [id:1...] ]
。【参考方案2】:
var subject = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
function chunksOfSize(source, size)
var arr = [];
for (var i = 0, count = source.length; i < count; i += size)
arr.push(arr.slice(i, i + size));
return arr;
console.log(chunksOfSize(subject, 4));
【讨论】:
需要使用像 1,2,3,4,5 这样的唯一集合来破坏数组。但这个 ids 可能会改变【参考方案3】:这样说:
0: candidateConfigId: "1", value: "199128700790"
1: candidateConfigId: "2", value: "Yujith"
2: candidateConfigId: "3", value: "Male"
3: candidateConfigId: "4", value: "SE"
4: candidateConfigId: "5", value: "Colombo5"
5: candidateConfigId: "1", value: "199128700792"
6: candidateConfigId: "2", value: "Yujith2"
7: candidateConfigId: "3", value: "Male"
8: candidateConfigId: "4", value: "SE"
9: candidateConfigId: "5", value: "Colombo3"
10: candidateConfigId: "1", value: "199128700793"
11: candidateConfigId: "2", value: "Yujith3"
12: candidateConfigId: "3", value: "Male"
13: candidateConfigId: "4", value: "SE"
14: candidateConfigId: "5", value: "Colombo2"
这个变量是:array
并且总是像发布的示例中那样排序(这是棘手的部分)。
在这种情况下,您可以使用这样的 reduce 函数:
let currentIndex = 0;
const Splitted = array.reduce((acc, cur) =>
acc[currentIndex].push(cur);
if (parseInt(cur.candidateConfigId) === 5) currentIndex++; acc[currentIndex] = []
return acc;
,[[]])
【讨论】:
其实没有,这个candidateConfigId可能会改变。但总会有独特的集合像 1,2,3,4,5 在元素的顺序正确之前,建议的解决方案应该可以工作 如果candidateConfigId 改变了怎么办。我们不能硬编码 5,因为 CandidateConfigId 可能会改变 你刚才说的正好相反:>但总会有唯一的集合,比如 1,2,3,4,5以上是关于JavaScript将数组拆分为内部的多个数组[重复]的主要内容,如果未能解决你的问题,请参考以下文章