【发布时间】:2020-06-09 15:34:58
【问题描述】:
我有一个对象数组
const objects = [a, b, c, d, e, f, g ... ]
我希望他们变成
const result = [a, [b, c], d, [e, f], g ... ]
有什么想法吗?
[编辑] 抱歉。这是我的第一篇文章,不知道我必须展示我的尝试。我也不认为我应该得到卑鄙的cmets,做个好人。我在头疼了 4 个小时后解决了它。这是我的解决方案:
const result = []
const method = array => {
for (let i = 0; i < array.length; i += 3) {
const set = new Set([array[i + 1], array[i + 2]])
if (i !== array.length - 1) {
result.push(array[i])
result.push(Array.from(set))
} else {
result.push(array[i])
}
}
}
感谢大家的回复!我读了每一本。
【问题讨论】:
-
你的尝试在哪里?
-
“请做我的功课”
-
一个快速的谷歌会回答这个问题。从字面上看,前 5 个结果显示了许多不同的选项。
标签: javascript arrays methods javascript-objects