gpt4 book ai didi

javascript - 将数组组分组为单独的子数组组

转载 作者:行者123 更新时间:2023-12-04 13:10:30 25 4
gpt4 key购买 nike

<分区>

我有一个带有字符串的 JS 数组,例如:

let a = ["a", "a", "a", "b", "c", "c", "b", "b", "b", "d", "d", "e", "e", "e"]

我需要比较数组中的重复字符串,如果存在重复字符串,它将像这样分隔:

[ ["a", "a", "a"], ["b"], ["c", "c"], ["b", "b", "b"], ["d", "d"], ["e", "e", "e"] ]

我试图将它与 for 循环进行比较,但我不知道如何编写代码以便数组检查自己的字符串是否重复,而没有预先确定的字符串进行比较。

let a = ["a", "a", "a", "b", "c", "c", "b", "b", "b", "d", "d", "e", "e", "e"];

let b = [];

let len = a.length;

for (let i = 0; i < len; i++) {
if (b.indexOf(a[i]) !== 1) {
b.push(a[i]);
}
}

console.log(b)

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com