function compareAllWithNext(arr: any) {
const resuits: any = [];
for (let i = 0; i < arr.length; i++) {
const current = arr[i];
const last = i == 0 ? -1 : arr[i - 1];
if (i == 0) {
resuits.push(">");
} else {
if (current > last) {
resuits.push(">");
} else if (current < last) {
resuits.push("<");
} else {
resuits.push("=");
}
}
}
return resuits;
}
// 示例使用
const numbers = [1, 2, 2, 4, 5, -1, 20];
console.log(compareAllWithNext(numbers)); // ['>', '>', '=', '>', '>', '<', '>']
js比较数组内下一元素跟当前元素
于 2024-05-09 16:02:08 首次发布