2017-10-06 42 views
1

我需要區分數組中的範圍,並將其展開。我正在使用JavaScript。區分數組中的範圍,並將它們展開

例:

var arr = ["1", "6", "4", "5", "9-11", "13-15"]; 

和所需的輸出

var output =["1", "6", "4", "5", "9", "10", "11", "13", "14", "15"]; 

我目前擁有的腳本來擴大在一定範圍內。我很難識別陣列中的範圍:

var range = "1-6"; 
range = str.split("-"); 

start = parseInt(range[0]); 
end = parseInt(range[1]); 

var rangeArray = []; 

for(var i = start; i <= end; i++){ 
    rangeArray.push(i); 
} 
console.log(rangeArray); 
+2

我想你必須以某種迭代數組,不能你只需檢查一個包含'-'元素,看它是否是一個範圍? 'element.includes(「 - 」)'(當range元素爲true時) – xander

回答

0

看起來你可以區分一個範圍,如果它包含「 - 」,否?

arr.forEach((val) => { 
    if (val.indexOf("-") > -1) { 
     // do something here 
    } 
}) 

對於// do something here,你會採取什麼你已經擁有並使其成爲一個功能:

function getArrayFromRange(range) { 
    range = str.split("-"); 

    start = parseInt(range[0]); 
    end = parseInt(range[1]); 

    var rangeArray = []; 

    for(var i = start; i <= end; i++){ 
     rangeArray.push(i); 
    } 

    return rangeArray; 
} 

你會調用該函數與您的範圍,然後從所返回什麼值插入進入arr的適當位置

1

試試這種方法。循環訪問數組並檢查該項是否包含-符號,如果是,則將其拆分並獲取範圍。遍歷範圍並將每個項目添加到數組中。否則,只需添加該項目。對於這種邏輯,您還可以創建一個幫助器函數,該函數返回給定範圍內的數組,並使用原始數組範圍返回concat

const arr = ["1", "6", "4", "5", "9-11", "13-15"]; 
 
let newArray = []; 
 

 
function getRange(start, count) { 
 
    const arr = []; 
 
    for(let i = start; i <= count; i++) { 
 
     arr.push(i.toString()); 
 
    } 
 
    return arr; 
 
} 
 

 
arr.forEach(item => { 
 
    if(item.includes('-')) {  
 
    const [start, end] = item.split('-').map(n => parseInt(n)); 
 
    newArray = newArray.concat(getRange(start,end)); 
 
    } else { 
 
    newArray.push(item); 
 
    } 
 
}); 
 

 
console.log(newArray);

您也可以做到這一點使用reduce方法

const arr = ["1", "6", "4", "5", "9-11", "13-15"]; 
 

 
function getRange(start, count) { 
 
    const arr = []; 
 
    for(let i = start; i <= count; i++) { 
 
     arr.push(i.toString()); 
 
    } 
 
    return arr; 
 
} 
 

 
const newArray = arr.reduce((arr, item) => { 
 
    if(item.includes('-')) { 
 
    const [start, end] = item.split('-').map(n => parseInt(n)); 
 
    arr = arr.concat(getRange(start, end)); 
 
    } else { 
 
    arr.push(item); 
 
    } 
 
    return arr; 
 
}, []); 
 

 
console.log(newArray);

1

我更新了你的代碼,使一個splitRanges函數返回人氣指數的所有號碼,並在最初的數組上循環使用該功能,以提取的所有範圍在每次迭代中:

var arr = ["1", "6", "4", "5", "9-11", "13-15"] 
 

 
function splitRanges(rangeStr) { 
 
    var range = rangeStr.split("-"); 
 
    var start = parseInt(range[0]); 
 
    var end = parseInt(range[1]); 
 
    var rangeArray = []; 
 

 
    for (var i = start; i <= end; i++) { 
 
    rangeArray.push(""+i+""); 
 
    } 
 
    return rangeArray; 
 
} 
 

 
var output = []; 
 

 
arr.forEach(function(r){ 
 
    if(r.indexOf("-")>-1){ 
 
     output = output.concat(splitRanges(r)); 
 
    }else 
 
     output.push(r); 
 
}); 
 
console.log(output);

說明:

此代碼循環的初始陣列上,並且在每次迭代中檢查它是否是一個範圍將此值傳遞給splitRanges函數,並將concat的結果傳遞給我們的輸出數組,否則只是push數組中的值output

0

您可以簡單地遍歷數組,並使用此代碼的一部分,當你有一個範圍:

var arr = ["1", "6", "4", "5", "9-11", "13-15"]; 
 
var output =[]; 
 

 
for(var j=0;j<arr.length;j++) { 
 

 
    if(arr[j].search("-")!=-1) { 
 
    range = arr[j].split("-"); 
 

 
    start = parseInt(range[0]); 
 
    end = parseInt(range[1]); 
 

 
    for(var i = start; i <= end; i++){ 
 
     output.push(i); 
 
    } 
 
    } else { 
 
    output.push(parseInt(arr[j])); 
 
    } 
 

 
} 
 
console.log(output);

0

// the predefined array 
 
var arr = ["1", "6", "4", "5", "9-11", "13-15"]; 
 

 
// new array will be this 
 
var output = []; 
 

 
// loop over each item in array 
 
for(i in arr){ 
 
    // if array has a '-' character in it 
 
    if(arr[i].indexOf('-') > 0){ 
 
    // split the array into start and end range 
 
    var split = arr[i].split('-'); 
 
    // loop through start and end 
 
    for(var p = parseInt(split[0]); p < parseInt(split[1]) + 1; p++){ 
 
     // push it to the new array 
 
     output.push(p.toString()); 
 
    } 
 
    }else{ 
 
    // the item doesn't contain '-', push it directly 
 
    output.push(arr[i].toString()); 
 
    } 
 
} 
 
console.log(output);

1

你可以這樣做像這樣:

let arr = ["1", "6", "4", "5", "9-11", "13-15"]; 
let out = []; 
const range = (start, end) => Array.from({length: (end - start)}, (v, k) => k + start); 
arr.forEach(item => { 
    let splitted = item.split('-'); 
    if(splitted.length > 1) { 
     out.push(...range(parseInt(splitted[0]), parseInt(splitted[1]) + 1)); 
    } else { 
     out.push(parseInt(item)) 
    } 
}) 
console.log('out: ', out); // [1, 6, 4, 5, 9, 10, 11, 13, 14, 15] 
0

對於我來說,它看起來像你這樣的事情之後,但也許我錯了:

var arr = ["1", "6", "4", "9-11", "13-15"]; 
 

 
var output =[ 
 
    [0, 1], 
 
    [0, 1, 2, 3, 4, 5, 6], 
 
    [0, 1, 2, 3, 4], 
 
    [9, 10, 11], 
 
    [13, 14, 15] 
 
]; 
 

 
var ranges = arr.map(function(item) { 
 
    var range = item.split('-'); 
 
    if (range.length === 2) { 
 
     var start = parseInt(range[0]); 
 
     var end = parseInt(range[1]); 
 
    } else { 
 
     start = 0; 
 
     end = parseInt(range[0]); 
 
    } 
 
    
 
    var rangeArray = []; 
 

 
    for(var i = start; i <= end; i++){ 
 
     rangeArray.push(i); 
 
    } 
 
    
 
    return rangeArray; 
 
});

0

你可能需要一段時間循環返回範圍的單一值。

var array = ["1", "6", "4", "5", "9-11", "13-15"], 
 
    result = array.reduce(function (r, a) { 
 
     var p = a .split('-'); 
 
     do { 
 
      r.push((p[0]++).toString()); 
 
     } while (p[0] <= p[1]) 
 
     return r; 
 
    }, []); 
 
    
 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

相關問題