2016-11-07 25 views
0

我有這樣的功能:如何獲得在陣列連續的空格的JavaScript

function insertValue2(cantidad, value, arr){ 

    var spaceCount = 0; 

    arr.forEach(function(item, index) { 
     if(item == "") { 
     console.log(index) 
     spacecount++; 
     } 
    }); 
    console.log(spaceCount) 

我的函數計算該數組中的空格:["", "", "", "B", "B", "B", ""]

因此,對於這樣的結果:0 1 2 6是在位置數組與空格和spacecount = 4

我不知道這是可能的,但任何想法如何計算空白空間之前,我得到第一個B?

我的意思是指望連續的空格一樣0 1 2 spacecount = 3再算上空間6 spacecount = 1

如果我想在我的陣列quantity = 1value = C,將選擇SPACECOUNT最低值插入。

["", "", "", "B", "B", "B", "C"]

編輯:

數量是我有多少空間陣列中使用,我不希望使用更多的空間比所需量

在這陣列我有位置空白0, 1, 2, 7 , 8, 9, 10

["", "", "", "B", "B", "B", "C", "" , "" ,"" ,""]

,如果我想插入quantity = 2value = D,預期的結果是:

["D", "D", "", "B", "B", "B", "C", "" , "" ,"" ,""]

如果我想插入quantity = 1 and value = "E"它會選擇position 2spacecount = 1保存了該 更高SPACECOUNT更大的量等3或4

["D", "D", "E", "B", "B", "B", "C", "" , "" ,"" ,""]

比ks的幫助:)

+0

我不明白你的例子與數量..所以你希望你的函數返回連續空格的數量和位置?告訴我們什麼是輸入功能,什麼是確切的期望輸出。 – Buksy

+0

輸出是存在的:所以結果爲:'0 1 2 6'是數組中空格的位置,'spacecount = 4'我不知道你不明白什麼:S – Eliott

+0

你在找一封信,或者僅僅是空間羣體? –

回答

0

這個建議首先查找任何空格,算上他們,並儘量減小在一個插槽中留下空的空間。然後將值應用於插槽。

function insert(array, value, count) { 
 
    var i, 
 
     spaces = data.reduce(function (r, a, i, aa) { 
 
      if (a === '') { 
 
       if (aa[i - 1] === '') { 
 
        r[r.length - 1].count++; 
 
       } else { 
 
        r.push({ index: i, count: 1 }); 
 
       } 
 
      } 
 
      return r; 
 
     }, []), 
 
     index = spaces.reduce(function (r, a) { 
 
      return a.count < count || r.count < a.count ? r : a; 
 
     }, {}).index; 
 

 
    if (index !== undefined) { 
 
     for (i = 0; i < count; i++) { 
 
      array[i + index] = value; 
 
     } 
 
    } else { 
 
     // throw no space error 
 
    } 
 
} 
 

 
var data = ["", "", "", "", "B", "B", "B", "C", "", "", ""]; 
 

 
insert(data, 'X', 5); // throw no space error, if implemented 
 
console.log(data); // ["", "", "", "", "B", "B", "B", "C", "", "", ""] 
 

 
insert(data, 'D', 2); 
 
console.log(data); // ["", "", "", "", "B", "B", "B", "C", "D", "D", ""] 
 

 
insert(data, 'E', 1); 
 
console.log(data); // ["", "", "", "", "B", "B", "B", "C", "D", "D", "E"]
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

非常感謝你!你是最好的!! – Eliott

0

嘗試使用計數器。

我用計數器檢查im是否在第一次空格或其他字符。如果counter = 0 - > quantity ++和counter = 1;此外,如果環路再次發現一個notwhitespace計數器變更爲0。

var arr = ["", "", "", "B", "B", "B", "C", "" , "" ,"" ,""]; 

var spacecount=0; 
var quantity=0; 
var contador=0; 

arr.forEach(function(item, index) { 
    if(item == "") { 
    if(contador==0){ 
     quantity++; 
     contador=1; 
     spacecount=0; 
    } 
    spacecount++; 
    }else{ 
    contador=0; 
    } 
}); 

console.log('spacecount: '+spacecount); 
console.log('quantity: '+quantity); 
+0

你在'if'和'else'之間有代碼 – Weedoze

+0

檢查{else else是否是第一個if和代碼inse是在第二個if之後。 – erNachete

+0

哎呀抱歉,縮進是凌亂的 – Weedoze

2

可以只是迭代這個數組一次,檢查是否項和數組的保持陣列,其中,每個產品具有所期望的基團的索引值,在你的情況下是空字符串。

的一種方法是通過擴展Array對象本身:(見結果控制檯)

Array.prototype.groupBy = function(value) { 
 
    var array = this; 
 
    var groups = []; 
 
    var buffer = []; 
 
    for (var i = 0; i < array.length; i++) { 
 
     var curItem = array[i]; 
 
     if (curItem == value) { 
 
      buffer.push(i); 
 
     } else if (buffer.length > 0) { 
 
      groups.push(buffer); 
 
      buffer = []; 
 
     } 
 
    } 
 
    if (buffer.length > 0) 
 
     groups.push(buffer); 
 
    return groups; 
 
}; 
 

 
var a = ["", "", "", "B", "B", "B", ""]; 
 
var consecutiveBlankSpaces = a.groupBy(""); 
 
console.log('total of ' + consecutiveBlankSpaces.length + ' groups of blank spaces'); 
 
for (var i = 0; i < consecutiveBlankSpaces.length; i++) { 
 
    console.log('Found a blank space group consisting of ' + 
 
     consecutiveBlankSpaces[i].length + ' items, indexes: ' + 
 
     consecutiveBlankSpaces[i]); 
 
}

+0

真的有用:) – Eliott

0

var arr = ["", "", "", "B", "B", "B", "C", "", "", "", ""]; 
 

 
var groups = {}; 
 
var groupsIndex = 0; 
 
var previouslyUpgraded = false; 
 

 
arr.forEach(function(item, index) { 
 
    if (item === "") { 
 
    groups["group" + groupsIndex] = groups["group" + groupsIndex] || []; 
 
    groups["group" + groupsIndex].push(index); 
 
    previouslyUpgraded = false; 
 
    } else if (!previouslyUpgraded) { 
 
    groupsIndex++; 
 
    previouslyUpgraded = true; 
 
    } 
 
}); 
 

 
console.log(groups); 
 
console.log(Object.keys(groups).length + " groups found !"); 
 

 
for (var key in groups) { 
 
    console.log(key + " has empty string at indexes : " + groups[key]); 
 
}
.as-console-wrapper { 
 
    max-height: 100% !important; 
 
    top: 0; 
 
}

0

function CountSpaces(a) { 
 
    var result = []; 
 
    var positions = []; 
 
    
 
    for(var i = 0, len = a.length; i<len; i++) { 
 
    if(a[i] != "" && positions.length > 0) { 
 
     result.push({ 
 
     positions: positions, 
 
     count: positions.length 
 
     }); 
 
     
 
     positions = []; 
 
     lastCharacter = a[i]; 
 
     continue; 
 
    } 
 
    
 
    if(a[i] == "") positions.push(i); 
 
    } 
 
    
 
    if(positions.length > 0) { 
 
    result.push({ 
 
     positions: positions, 
 
     count: positions.length 
 
    }); 
 
    } 
 
    
 
    return result; 
 
} 
 

 
var a1 = ["", "", "B", "", "", "C", "D", "", "", ""]; 
 
console.log(a1, CountSpaces(a1));