简体   繁体   中英

JavaScript finding matching values in a for loop

The array is sorted in an increasing order and I would like to get the values of the matching ones separated by a colon.

var array = [1, 1, 2, 2, 2, 3, 4, 5, 5, 5, 5];
var text = "";

for(var i = 0; i < array.length - 1; i++) {
    if(array[0] == array[0 + 1]) {
         text = array[0] + ":" + array[0 + 1]; // 1:1
    }
}

This for-loop only checks for two matching values. How would I check for all the number of matching values?

So for 1, the text would be 1:1
for 2, the text would be 2:2:2
for 5, the text would be 5:5:5:5

 var array = [1, 1, 2, 2, 2, 3, 4, 5, 5, 5, 5] var text = '' for(var i = 0; i < array.length; i++) { if (array[i] == array[i + 1]) { // always add a : suffix text += array[i] + ':'; } else { // replace the last character with the last value and a new line text = text.replace(/.$/, ':' + array[i] + '\\n') } } console.log(text.trim()) // trim the trailing whitespace 

 var array = [1, 1, 2, 2, 2, 3, 4, 5, 5, 5, 5], arrayLength = array.length, text = {}; for(var i = 0; i < arrayLength; i++) { if(!text.hasOwnProperty(array[i])){ text[array[i]] = [] for(var e = 0; e < arrayLength; e++){ if(array[i] == array[e]){ text[array[i]].push(array[i]) } } text[array[i]] = text[array[i]].join(':') } } //and now you can access to every string by text[number] for(var key in text){ console.log(text[key]) } 

I'm not quite sure what you're trying to achieve. I hope this does what you're looking for.

 const input = [1, 1, 2, 2, 2, 3, 4, 5, 5, 5, 5], // Based on the input create a set, each value will appear only once in // the set. uniqueIds = new Set(input), result = []; // Iterate over all the unique values of the array. uniqueIds.forEach(id => { // Add the text for the current value to result array. result.push( // Filter the array, take only the items that match the current value. This // will result in a new array that can be joined using a ":" as a // separator character. input.filter(value => value === id).join(':') ); }); // Or on a single line: // uniqueIds.forEach(id => result.push(input.filter(value => value === id).join(':'))); console.log(result); 

If you want to get a string based on an input you provide, perhaps this is better:

 const input = [1, 1, 2, 2, 2, 3, 4, 5, 5, 5, 5], textWrapper = (function() { let array = null, cache = {}; function getText(number) { if (cache[number] !== undefined) { console.log(`Cache hit for number ${number}`); return cache[number]; } const text = array.filter(item => item === number).join(':'); cache[number] = text; return text; } function setArray(value) { array = value; cache = {}; } return { getText, setArray } })(); textWrapper.setArray(input); const values = [1, 3, 5, 5]; values.forEach(value => console.log(`Text for ${value} is "${textWrapper.getText(value)}"`)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM