我正在嘗試制作一個["1-5"]如果我給出回傳的程式[1,2,3,4,5]。
我已經做到了,但我無法過濾它。所以我想要一個可以過濾我的輸出代碼的代碼。或者任何比我更好的代碼。
let array = [1,2,3,5,6,7,8,10,11, 34, 56,57,];
let x = [];
for(let i = 0; i < array.length; i ){
for(let j = 0; j < array.length; j ){
if(array[i] j == array[j]){
x.push(array[i] "-" array[j]);
}
if(array[j] > array[i] j && array[j 1]){
let y = array.slice(j, array.length)
array = y;
i, j = 0;
}
if(array[i] - array[i 1] != -1 && array[i 1] - array[i] != 1 && array[i 1] != undefined){
x.push(array[i]);
}
}
}
console.log(x);
uj5u.com熱心網友回復:
如果假設串列是有序的,我們只需要順序遍歷串列即可。沒有必要有雙重嵌套回圈。如果您保持足夠的狀態,您可以確定您是否在一個組中,并且您只管理組中的開始與最后一個元素。
為了簡化事情,我使用了 ES6 字串插值${start}-${last}。
let array = [1,2,3,5,6,7,8,10,11, 34, 56,57];
let result = [ ];
let hasStart = false;
let start = 0;
let last = 0;
for (let num of array) {
if (!hasStart) {
hasStart = true;
last = start = num;
continue;
}
if (num === last 1) {
last = num;
continue;
}
result.push( start === last ? start : `${start}-${last}` );
last = start = num;
}
if (hasStart) {
result.push( start === last ? start : `${start}-${last}` );
}
console.log(result);
uj5u.com熱心網友回復:
輸入:[1,2,3,4,5]
輸出:[“1-5”]
所以我假設你想獲取格式的字串:
["smallestelement-largestelement"]
var input1 = [1,2,3,4,5]
console.log( "[" '"' Math.min(...input1) "-" Math.max(...input1) '"' "]")
如果你想要的是格式的字串:
["firstelement-lastelement"]
var input1 = [1,2,3,4,5]
console.log( "[" '"' input1[0] "-" input1.pop() '"' "]")
uj5u.com熱心網友回復:
let min = Number.MAX_VALUE
let max = Number.MIN_VALUE
arr.forEach((element) => {
if(element > max) max = element
if(element < min) min = element
}
console.log(`${min} - ${max}`)
uj5u.com熱心網友回復:
如果你有一個整數陣列,并且如果你想輸出范圍,你可以原生sort()它(你也可以提供排序規則)并shift()用于第一個元素和slice(-1)最后一個元素:
let arr = [4,1,5,3].sort();
console.log(arr.shift() '-' arr.slice(-1));
正如評論中所說,您應該澄清您是否希望"1-57"使用片段陣列,或者更廣泛地描述您的用例。
uj5u.com熱心網友回復:
問題的措辭使這有點難以回答,但根據您的代碼片段,我可以推測您是:
- 試圖找到整個陣列的范圍或
- 嘗試在陣列中查找連續范圍
根據這些解釋,您可以如下回答這個問題:
function detectRange(a) {
// clone a
const b = [...a]
// remove first value
const min = max = b.splice(0, 1)[0]
// compute range
const range = b.reduce(({min, max}, i) => {
if(i < min) min = i
if(i > max) max = i
return { min, max }
}, {min, max})
return range
}
function detectRanges(a) {
// clone a
const b = [...a]
// remove first value
const min = max = b.splice(0, 1)[0]
// init ranges array
const ranges = [ ]
// compute ranges
const range = b.reduce(({min, max}, i) => {
if(i === max 1) {
return {min , max: i}
} else {
ranges.push({min, max})
return {min: i, max: i}
}
}, {min, max})
// push the remaining range onto the array
ranges.push(range)
return ranges
}
function printRange(r) {
console.log(`["${r.min}-${r.max}"]`)
}
function printRanges(r) {
r.forEach(i => {
printRange(i)
})
}
// detect and print range of whole array
printRange(detectRange([1, 2, 3, 5, 6, 7, 8, 10, 11, 34, 56, 57]))
// detect and print only contiguous ranges within array
printRanges(detectRanges([1, 2, 3, 5, 6, 7, 8, 10, 11, 34, 56, 57]))
uj5u.com熱心網友回復:
const array = [1, 2, 3, 5, 6, 7, 8, 10, 11, 34, 56, 57];
let s = null, d = 0;
const result = array.sort((a, b) => a - b).reduce((p, c, i, arr) => {
d ;
if (!s) s = c;
if (arr[i 1] - s > d) {
s === c ? p.push(s) : p.push(`${s}-${c}`);
s = null;
d = 0;
}
if (!arr[i 1]) s === c ? p.push(s) : p.push(`${s}-${c}`);
return p;
}, []);
console.log(result);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/532456.html
上一篇:僅當它具有值時,Wordpress后網格才按自定義欄位排序
下一篇:按動態鍵對多維陣列進行排序
