简体   繁体   English

JavaScript在for循环中找到匹配的值

[英]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. 此for循环仅检查两个匹配值。 How would I check for all the number of matching values? 如何检查所有匹配值的数量?

So for 1, the text would be 1:1 因此,对于1,文本将为1:1
for 2, the text would be 2:2:2 对于2,文本将为2:2:2
for 5, the text would be 5:5:5:5 对于5,文字将为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)}"`)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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