2017-09-28 13 views
0

配列は昇順に並べ替えられ、一致するものの値をコロンで区切って取得したいと考えています。forループで一致する値を見つけるJavaScript

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 
    } 
} 

このforループは、2つの一致する値のみをチェックします。一致するすべての値の数を確認するにはどうすればよいですか?

だから、1のために、テキストは1次のようになります。2:1
2のために、テキストが2となる5:5:5

+1

あなたは巣に私は非常にあなたが達成したいものを得ることはありませんループ – yBrodsky

+0

が必要になります。なぜすべてを同じテキスト変数に格納していますか?配列をテキストとして出力したい場合は、より簡単な方法があります。あなたがそれぞれの数字のテキストを保存したい場合配列では、これはArray.reduce()ジョブのように聞こえる –

答えて

-1

I 2
5のために、テキストは5になりますあなたが達成しようとしていることはかなりわかりません。私はこれがあなたが探していることを望むことを願っています。

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);

あなたが提供した入力に基づいて文字列を取得したい場合は、おそらくこれは良いです:

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)}"`));

0

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]) 
 
}

1

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

関連する問題