gpt4 book ai didi

Javascript 数组以连续数字等开头单词进行分割

转载 作者:行者123 更新时间:2023-12-02 18:38:12 27 4
gpt4 key购买 nike

你能帮我吗?我有一个名为 baseArray 的数组。正如您所看到的一些值是相同的,我尝试删除重复的值,但没有完全起作用。

然后我尝试分割数组,例如

[[1,2,3,4,5,6,7], [1,2,4,5,6,7], [1,2,3,4,5,6,7,8,9,10..]..]

我想分割以数字 1 开头的每个数字系列

const editedArray = [];

const baseArray = [1, 2, 3, 4, 5, 6, 7, 7, 7, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 12, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7,
7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
13, 13, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12,
12, 13, 13, 1, 2, 3, 4, 5, 6, 6, 6, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 10, 10, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10,
10, 1, 2, 3, 4, 5, 6, 6, 6, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 1, 2, 3, 4, 5, 6, 7,
8, 9, 9, 9, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 10, 10, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 11, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7,
8, 8, 9, 9, 10, 10, 11, 11, 1, 2, 3, 4, 5, 5, 5, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5
]
baseArray.filter(function (item, pos) {
return baseArray.indexOf(item) == pos;
})

baseArray.forEach(item => item === 1 ?
editedArray.push([1]) :
editedArray[editedArray.length - 1].push(item)
)

console.log(editedArray)

这给了我这样的结果 enter image description here

已更新,现在可以正常使用了

const editedArray = [];

const baseArray = [1, 2, 3, 4, 5, 6, 7, 7, 7, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 12, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7,
7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
13, 13, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12,
12, 13, 13, 1, 2, 3, 4, 5, 6, 6, 6, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 10, 10, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10,
10, 1, 2, 3, 4, 5, 6, 6, 6, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 1, 2, 3, 4, 5, 6, 7,
8, 9, 9, 9, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 10, 10, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 11, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7,
8, 8, 9, 9, 10, 10, 11, 11, 1, 2, 3, 4, 5, 5, 5, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5
]
var result = baseArray.filter((a, i, aa) => a !== aa[i + 1]);

result.forEach(item => item === 1 ?
editedArray.push([1]) :
editedArray[editedArray.length - 1].push(item)
)
console.log(editedArray)

最佳答案

使用Array#reduce

const baseArray = [1,2,3,4,5,6,7,7,7,1,1,2,2,3,3,4,4,5,5,6,6,7,7,1,2,3,4,5,6,7,8,9,10,11,12,12,12,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,1,2,3,4,5,6,7,8,9,10,11,12,13,13,13,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,1,2,3,4,5,6,6,6,1,1,2,2,3,3,4,4,5,5,6,6,1,2,3,4,5,6,7,8,9,10,10,10,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,1,2,3,4,5,6,6,6,1,1,2,2,3,3,4,4,5,5,6,6,1,2,3,4,5,6,7,8,9,9,9,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,1,2,3,4,5,6,7,8,9,10,10,10,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,1,2,3,4,5,6,7,8,9,10,11,11,11,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,1,2,3,4,5,5,5,1,1,2,2,3,3,4,4,5,5];

const editedArray = baseArray.reduce((acc, num, i, arr) => {
if (arr[i - 1] !== num) {
if (num === 1) acc.push([num]);
else acc[acc.length - 1].push(num);
}

return acc;
}, []);

console.log(editedArray);

关于Javascript 数组以连续数字等开头单词进行分割,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68437560/

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