用连续数字等开头单词拆分Javascript数组



你能帮我吗?我有一个名为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)

这让我觉得在此处输入图像描述

更新,现在工作正常

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);

以下示例:

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 output = baseArray
.join(",")
.split(",1,")
.map(el => Array.from(new Set(("1," + el).split(","))));
console.log(output);

您的过滤器错误。你可以使用

filter((item, pos) => !pos || baseArray[pos - 1] !== item)

删除连续的重复:

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.filter((item, pos) => !pos || baseArray[pos - 1] !== item)
.forEach(item => item === 1 ?
editedArray.push([1]) :
editedArray[editedArray.length - 1].push(item)
)
console.log(editedArray);

您必须删除filter语句,在第一个forEach之后,您可以为editedArray使用另一个forEah,并从editedArray中的每个数组中删除重复元素:

const finalArray = [];
editedArray.forEach(item => {
finalArray.push([...new Set(item)]);
})

我想这个解决方案会起作用。这是一个单独的循环。

let editedArr = [];
let subArr = [];
let hash = {};
baseArray.forEach((item, index) => {
if(index) {
/* If you want to remove 1 as a array */
/* Change the below condition to */
/* if((item === 1 && baseArray[index - 1] !== 1) || index === baseArray.length - 1) */
if(item === 1 || index === baseArray.length - 1) {
editedArr.push(subArr);
subArr = [item];
hash = {
[item]: true,
};

} else if (!(item in hash)) {
subArr.push(item);
hash[item] = true;
}
} else {
subArr.push(item);
hash[item] = true;
}
});
console.log({editedArr});

最新更新