2016-06-08 59 views
1

這裏是我的JavaScript數組:計數獨特的出現在陣列中包含的JavaScript特定字符串的數

arr = ['blue-dots', 'blue', 'red-dots', 'orange-dots', 'blue-dots']; 

使用JavaScript,我怎麼能算陣列中的所有唯一值的總數包含字符串「點」。所以,對於上面的數組,答案將是3(藍點,橙點和紅點)。

+2

使用常規引號[在陣列唯一值]的 – JordanHendrix

+0

可能的複製(http://stackoverflow.com/questions/1960473/unique-values-in-an-array) –

+0

這個其他問題沒有解釋如何也只計算包含字符串的元素。 –

回答

2
var count = 0, 
    arr1 = [];  
for (var i = 0; i < arr.length; i++) { 
    if (arr[i].indexOf('dots') !== -1) { 
     if (arr1.indexOf(arr[i]) === -1) { 
      count++; 
      arr1.push(arr[i]); 
     } 
    } 
} 

你檢查是否有一定的元素包含「點」,如果確實如此,你檢查它是否已經在ARR1,如果沒有增量次數,並添加元素ARR1。

0

this question,我得到了getUnique函數。

Array.prototype.getUnique = function(){ 
    var u = {}, a = []; 
    for(var i = 0, l = this.length; i < l; ++i){ 
     if(u.hasOwnProperty(this[i])) { 
     continue; 
     } 
     a.push(this[i]); 
     u[this[i]] = 1; 
    } 
    return a; 
} 

話可以加計數字符串的ocurrences字符串數組裏面的函數:

function getOcurrencesInStrings(targetString, arrayOfStrings){ 
    var ocurrencesCount = 0; 
    for(var i = 0, arrayOfStrings.length; i++){ 
    if(arrayOfStrings[i].indexOf(targetString) > -1){ 
     ocurrencesCount++; 
    } 
    } 
    return ocurrencesCount; 
} 

那麼你只需要:

getOcurrencesInStrings('dots', initialArray.getUnique()) 

這將返回你要的號碼。

這不是最小的一段代碼,但它是高度可重用的。

1

一種方式是存儲元素作爲對象的鍵,然後拿到鑰匙的計數:

var arr = ["blue-dots", "blue", "red-dots", "orange-dots", "blue-dots"]; 
 
console.log(Object.keys(arr.reduce(function(o, x) { 
 
    if (x.indexOf('dots') != -1) { 
 
    o[x] = true; 
 
    } 
 
    return o 
 
}, {})).length)

+0

優雅,但可以改善,像它+1 –

0

試試這個是這樣的:

// Create a custom function 
function countDots(array) { 
    var count = 0; 

    // Get and store each value, so they are not repeated if present. 
    var uniq_array = []; 
    array.forEach(function(value) { 
    if(uniq_array.indexOf(value) == -1) { 
     uniq_array.push(value); 

     // Add one to count if 'dots' word is present. 
     if(value.indexOf('dots') != -1) { 
     count += 1; 
     } 
    } 
    }); 

    return count; 
} 

// This will print '3' on console 
console.log(countDots(['blue-dots', 'blue', 'red-dots', 'orange-dots', 'blue-dots'])); 
0

var uniqueHolder = {}; 
 
var arr = ["blue-dots", "blue", "red-dots", "orange-dots", "blue-dots"]; 
 
arr.filter(function(item) { 
 
    return item.indexOf('dots') > -1; 
 
    }) 
 
    .forEach(function(item) { 
 
    uniqueHolder[item] ? void(0) : uniqueHolder[item] = true; 
 
    }); 
 
console.log('Count: ' + Object.keys(uniqueHolder).length); 
 
console.log('Values: ' + Object.keys(uniqueHolder));

0

var arr = [ "blue-dots", "blue", "red-dots", "orange-dots", "blue-dots" ]; 
 
var fArr = []; // Empty array, which could replace arr after the filtering is done. 
 
arr.forEach(function(v) { 
 
    v.indexOf("dots") > -1 && fArr.indexOf(v) === -1 ? fArr.push(v) : null; 
 
    // Filter if "dots" is in the string, and not already in the other array. 
 
}); 
 

 
// Code for displaying result on page, not necessary to filter arr 
 

 
document.querySelector(".before").innerHTML = arr.join(", "); 
 
document.querySelector(".after").innerHTML = fArr.join(", ");
Before: 
 
<pre class="before"> 
 
    </pre> 
 
After: 
 
<pre class="after"> 
 
    </pre>

爲了把這個簡單的說,它會遍歷數組,如果點是在字符串中,它不會在已經存在fArr,它會推入fArr ,否則它什麼都不會做。

0

試試這個代碼,

arr = ["blue-dots", "blue", "red-dots", "orange-dots", "blue-dots"]; 

sample = [];  
for (var i = 0; i < arr.length; i++) { 
    if ((arr[i].indexOf('dots') !== -1) && (sample.indexOf(arr[i]) === -1)){ 
     sample.push(arr[i]); 

    } 
} 

alert(sample.length); 
0

我分開的字符串比較並返回獨特的項目的操作,使你的代碼更易於測試,讀取和重用。

var unique = function(a){ 
 
    return a.length === 0 ? [] : [a[0]].concat(unique(a.filter(function(x){ 
 
     return x !== a[0]; 
 
    }))); 
 
}; 
 

 
var has = function(x){ 
 
    return function(y){ 
 
     return y.indexOf(x) !== -1; 
 
    }; 
 
}; 
 

 
var arr = ["blue-dots", "blue", "red-dots", "orange-dots", "blue-dots"]; 
 
var uniquedots = unique(arr.filter(has('dots'))); 
 
console.log(uniquedots); 
 
console.log(uniquedots.length);

相關問題