【发布时间】:2021-08-21 09:35:03
【问题描述】:
如何从多个动态数组中复制不重复的元素
我想让每个 graphmonthdata 的长度相同。 例如 graphmonthdata[2] 长度为 4,所以我想让它的长度以嵌套数组中的最高长度为准,而另一个嵌套数组中缺少值,反之亦然。 基本上所有嵌套数组都应该具有相同的长度和相同的元素,方法是从另一个嵌套数组中添加缺失的元素
在 javascript 或 c# 中请帮助谢谢!
示例嵌套数组
graphmonthdata = [
[
"Bug Fixing",
"Deployment",
"Design",
"Development",
"Documentation",
"Downtime",
"Learning & Skill Upgrade",
"Meetings",
"Project Management",
"Requirement Understanding",
"Review",
"Support",
"Testing"
],
[
"Bug Fixing",
"Deployment",
"Development",
"Documentation",
"Meetings",
"Review",
"Support",
"Testing",
"UAT/Prod. Fixes"
],
[
"Meetings",
"Organisational Activity",
"Planned/Unplanned Leave",
"Testing"
]
]
最终的数组应该是这样的
graphmonthdata =
[
[
"Bug Fixing",
"Deployment",
"Design",
"Development",
"Documentation",
"Downtime",
"Learning & Skill Upgrade",
"Meetings",
"Organisational Activity",
"Planned/Unplanned Leave",
"Project Management",
"Requirement Understanding",
"Review",
"Support",
"Testing",
"UAT/Prod. Fixes"
],
[
"Bug Fixing",
"Deployment",
"Design",
"Development",
"Documentation",
"Downtime",
"Learning & Skill Upgrade",
"Meetings",
"Organisational Activity",
"Planned/Unplanned Leave",
"Project Management",
"Requirement Understanding",
"Review",
"Support",
"Testing",
"UAT/Prod. Fixes"
],
[
"Bug Fixing",
"Deployment",
"Design",
"Development",
"Documentation",
"Downtime",
"Learning & Skill Upgrade",
"Meetings",
"Organisational Activity",
"Planned/Unplanned Leave",
"Project Management",
"Requirement Understanding",
"Review",
"Support",
"Testing",
"UAT/Prod. Fixes"
]
]
【问题讨论】:
-
使用Set。
-
如果最大的内部数组不包含 smaller 数组中的项目怎么办。例如。
[['a','b','c'], ['a'], ['b'], ['d']]。在这种情况下应该是什么输出? -
@YevgenGorbunkov 它应该填充较小数组中的缺失值,例如这里最长的数组是 graphmonthdata[0] 没有来自 graphmonthdata[2] 的值,即“Planned/Unplanned Leave”所以我们最长的需要加上“Planned/Unplanned Leave”。希望这可以消除您的疑问
-
如果您需要从所有嵌套数组中获取所有唯一值,只需使用
Set和Array.prototype.flat():const uniqueValues = [...new Set(graphmonthdata.flat())]
标签: javascript c# arrays typescript arraylist